Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6087637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:53:34+00:00 2026-05-23T11:53:34+00:00

I have two classes A and B as below public class A { Number

  • 0

I have two classes A and B as below

public class A {
    Number x;

    public A (){
        x = 10;
    }

    public void setX(Number x){

        //do a bunch of super complicated and lengthy stuff
        // afterwards set x
        this.x = x;
    }
}

public class B extends A{
    int x;

    public B(){
        super();
    }

    public void setX(int x){
        super.setX(x);
    }

    public int getX(){
        return x;
    }
}

and my Main is

public class Main {
    public static void main(String[] args) {
        B test = new B();
        test.setX(9);
        System.out.println(test.getX());
    }
}

The output is 0. I want it to be 9. I realize that in this trivial example I could just write x = 9 in my method in B, but if it were a more complicated method where I didn’t want to rewrite all the code that was already written in my superclass method then how would I make that happen?

EDIT: in subclass B I am purposely calling my int variable x to hide the superclass variable x. Pretend the setX method in the superclass does a ton of other stuff before finally setting x. I want it to set the x of the subclass instead. is this possible? This is a trivialized example. In my actual problem it’s much more complicated.

EDIT: actual problem is this. I have a class called ColorBinaryTree which is a subclass of BinaryTree. the only difference is the ColorBinaryTree class has a field for color so I can implement a red-black tree. The BinaryTree class references to parent, left, and right which are all BinaryTree objects. The ColorBinaryTree class has the same references but they are ColorBinaryTree objects instead of BinaryTree objects. I have a bunch of methods which manipulate the tree. One example would be the following

public BinaryTree<E> root() {
    if (parent == null) return this;
    else return parent.root();
}

In my subclass I need to override this so I can do covariant return type and return a ColorBinaryTree object. But it would be nice if i could call the superclass method inside my subclass method. But it seems that if I call the superclass method it looks at the superclass’s parentfield. I want it to look at the subclass’s parent field.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:53:35+00:00Added an answer on May 23, 2026 at 11:53 am

    As you use generics, you use Java 5. What about overriding the methods with a more specific return type:

    class BinaryTree<E> {
      public BinaryTree<E> root() {
        if (parent == null) return this;
        else return parent.root();
      }
    }
    
    class ColorBinaryTree<E> extends BinaryTree<E> {
      @Override
      public ColorBinaryTree<E> root() {
        return (ColorBinaryTree<E>)super.root();
      }
    }
    

    Or may use a more sophisticated generic solution:

    abstract class AbstractBinaryTree<E, T extends AbstractBinaryTree<E,T>> {
      T parent;
    
      public T root() {
        if (parent == null) return (T)this;
        else return parent.root();
      }
    }
    
    class BinaryTree<E> extends AbstractBinaryTree<E, BinaryTree<E>>{
    }
    
    class ColorBinaryTree<E> extends AbstractBinaryTree<E, ColorBinaryTree<E> {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question, here are two classes below: class Base{ public: virtual void
I have two classes as below. public class Destination { public Destination() { _StringCollection
I have two classes, shown below: [Serializable] [XmlInclude(typeof(SomeDerived))] public class SomeBase { public string
I have defined two classes below: public class junk { private BigInteger a =
I have two classes: public class MyBase { public virtual void DoMe() { }
I have two classes declared as below: class User { public: MyMessageBox dataMsgBox; };
I have two classes as below: [DataContract] public class Address { [DataMember] public string
I have two classes below: public class Module { public int Id { get;
I have two repository classes below, MoneyTransferRepository class is used by other classes in
Suppose we have declared these two classes: public class Animal { //..... } public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.