As you use generics, you use Java 5. What about overriding the methods with a more specific return type: class BinaryTree { public BinaryTree root() { if (parent == null) return this; else return parent.root(); } } class ColorBinaryTree extends BinaryTree { @Override public ColorBinaryTree root() { return (ColorBinaryTree)super.root(); } } Or may use a more sophisticated generic solution: abstract class AbstractBinaryTree> { T parent; public T root() { if (parent == null) return (T)this; else return parent.root(); } } class BinaryTree extends AbstractBinaryTree{ } class ColorBinaryTree extends AbstractBinaryTree.
Let's start off with the fact that the two variables are named x - that's a complete coincidence as far as the compiler's concerned. It's not like B. X "overrides A.x.
If you want polymorphic behaviour, you've got to use methods. Now you could override setX in B: @Override public void setX(Number x) { this. X = x.intValue(); } You might also want to make it call super.setX().
It would help if you could give a more realistic example though - I'd avoid a design like this to start with...
– jhlu87 Jun 29 at 19:27 @jhlu87: Well it effectively hides it within the code in B. Frankly it shouldn't usually matter, as usually your fields should be private to start with, so you shouldn't have access to your superclass's fields. – Jon Skeet Jun 29 at 19:29 @Jon Skeet, please see my additional edit.
– jhlu87 Jun 29 at 19:36 1 @jhlu87: ding superclass fields is not going to help you. Having two variables which contain effectively the same data is a bad idea. – Jon Skeet Jun 29 at 19:46 @Jon Skeet, if what you say is true then there is really no point for me to make ColorBinaryTree a subclass of BinaryTree.
Still it seems if I do do it this way I can at least take advantage of a methods that do rotations. It seems wrong to have to copy and paste so much code when the only difference is just the addition of one String field. – jhlu87 Jun 29 at 19:51.
You've re-defined x in your child class. Take the int x declaration out and remove the "super. " part of the setX method so it just reads: "this.
X = x". The root of your problem, is you are changing the x defined in class A, then providing the value in class B which was initialized to 0.
You ouput the private int x from the subclass and set the Number variable in the superclass.
Remove the "x" variable from your class B. Your method getX() is returning that and not the one in the base class.
What about this solution without an own member (as long as x in A is not private): public class B extends A { public void setX(int x){ super. SetX(x); } public int getX(){ return x.intValue(); } } It is never a good design to have equal named members in a class hierarchy!
See my edit on the bottom for my more complicated actual problem. I had that originally, but I need the tree references to also be ColorBinaryTree objects. That's why I declared them again.
I used the same name in hopes of hiding the references in the superclass so I can take advantage of inherited methods. – jhlu87 Jun 29 at 19:56 "I need the tree references to also be ColorBinaryTree objects. " Why?
– Karl Knechtel Jun 29 at 20:15.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.