output i got after running C4 -
a in c3 is30
a in c1 is10
Q1- I believe the met1() in all the three classes are different.
please correct me if i'm wrong in my understanding
Q2- in the case of above statement being true
i thought the output should be either
a in c3 is30
a in c2 is20
as far as i understood java till now - at runtime in polymorphism the actual object method runs not reference's method
(or )
a in c1 is10
a in c1 is10
as i thought may be because all the three met1() methods are different C1 's met1() should run
please do correct me where ever i understood java wrongly...
Thanks in advance
ajith
(hurray i posted my first Q in java ranch)
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
Welcome to the Ranch
Please use the Code button otherwise your code is illegible.
That is a good example of confusing code. You say you have class C2 and then write C4. Writing C1 C2 C3 and C4 is guaranteed to confuse people. Also some bad design, with public fields.
The real problem is that the method in C2 does not override that in C1. You can try adding the @Override annotation to C2#met1. The explanation is in your own post . . .
In all classes met1() function uses default (package-private) specifier which is visible to the class and its own package. As C1 and C2 classes are under different package, C2.met1() not overiding the function from base class C1.met1(). On invoking o2.met1(); it invokes C1.met1() function.
Kuldip
ajithkumar punnakula
Greenhorn
Joined: Dec 27, 2008
Posts: 8
posted
0
hi
@Kuldip Shetty
thanks for your time..thanks
@ Campbell Ritchie
hey thanks for your time..
Sorry for confusing C2 and C4....
Will use code button next time...did not know about the code button while i was posting my Q.
Thanks