Re: Late binding problem ?
Quote:
What amazes me the most is the fact that if i add System.out.println(this.getClass().getName()); in the getAnotherValue(...) method the output is class2.
Why does this really amaze you? You are calling it from class C2's object, also you have never referred C1 to C2. And it's calling
Code :
public int visit ( class1 c1) {
System.out.println("not here");
return c1.getValue();
}
this as you are calling it inside the class1.
I know i am not that descriptive but i don't know how to explain it any further. Any one else can explain here too please. Thanks
Re: Late binding problem ?
So how can i make it to be called from where i want without using the copy-pasting method of the same method in class2 ?
Re: Late binding problem ?
Quote:
Originally Posted by
sanguinarius
So how can i make it to be called from where i want without using the copy-pasting method of the same method in class2 ?
As far as i know, you misunderstood the concept. Let's make it easy. What do you want to do? You are passing the value of class2 object and you want program to call method accepting class1's object? How do you really think, compiler knows, what you want to do unless you specify it. Just pass it the class1's object and it will do what you want it to do.
Re: Late binding problem ?
I want to pass a value of class2 to the method getAnotherValue() and i want it to make a call to public int visit ( class2 c2 ) and not public int visit ( class1 c1) as it does now. I believe that is the normal way how things should work. But they don't.
Re: Late binding problem ?
Okay, so the reason might be , as you are using value variable of class1, as you can see class2 only extends and sets this value , it doesn't have it's own variable. So, i guess (I am not pretty sure) that when you call
it takes "this" as type Class1 not Class2 and that's somehow seems you weird. Why don't you try creating a variable for class2 named value2 and set it's value and then try calling the same function.