| Author |
which method is invoked and why?
|
Abhishek Reddy
Ranch Hand
Joined: Mar 28, 2006
Posts: 259
|
|
In the above code i typecasted the "c" reference to A type. When i invoke the method meth() on "a" reference method meth() of class B is invoked rather than Class A method..why is that so.. [ September 29, 2006: Message edited by: Barry Gaunt ]
|
Abhishek
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
Originally posted by Abhishek Reddy Chepyala: class A { protected void meth() { System.out.println("meth() of A called"); } } class B extends A { protected void meth() { System.out.println("meth() of B called"); } } class C extends B { public static void main( String args[]) { C c=new C(); A a=(A)c; a.meth();//------which method is invoked and why- System.out.println("exited main"); } } In the above code i typecasted the "c" reference to A type. When i invoke the method meth() on "a" reference method meth() of class B is invoked rather than Class A method..why is that so..
Ok Let me explain, Please correct me other ranchers If I am wrong, Prior to explain this I would like to introduce one example What do you think the output is? Correct Output is: in child Anyways now as you see in the your code that C is direct subclass of Class B So it will search the called method in it body itself but if it failed then it goes into the direct superclass that is B There it found the method so it executes the method given in Class B It is the exmple of run Time Binding. Check this out PolyMorphism-Run Time Binding
|
The Best way to predict your future is to create it
Ankur Sharma
|
 |
Abhishek Reddy
Ranch Hand
Joined: Mar 28, 2006
Posts: 259
|
|
public class Sample extends ABC { public void add() { System.out.println(" in child"); } public static void main(String arg[]) { ABC a = new Sample(); a.add(); } } class ABC { public void add() { System.out.println(" in Parent"); } } At runtime reference "a" is pointing to SampleObject so, the behaviour..its fine....now tell me what happens when we typecast the refrence "a" to ABC type ie ABC a1=(ABC)a;...will it still point to Sample object..
|
 |
Ankur Sharma
Ranch Hand
Joined: Dec 27, 2005
Posts: 1234
|
|
Originally posted by Abhishek Reddy Chepyala: public class Sample extends ABC { public void add() { System.out.println(" in child"); } public static void main(String arg[]) { ABC a = new Sample(); a.add(); } } class ABC { public void add() { System.out.println(" in Parent"); } } At runtime reference "a" is pointing to SampleObject so, the behaviour..its fine....now tell me what happens when we typecast the refrence "a" to ABC type ie ABC a1=(ABC)a;...will it still point to Sample object..
Yes ofcourse it will still the same Sample Object. Run this code and then check. Again the same output is there: in child. Please let me know if there are any issues. [ September 29, 2006: Message edited by: Ankur Sharma ]
|
 |
Joe Harry
Ranch Hand
Joined: Sep 26, 2006
Posts: 8795
|
|
Hi Ankur, So casting is applicable only to references and not for actual objects...am I right??Please clarify. Regards, Jothi Shankar Kumar. S
|
SCJP 1.4, SCWCD 1.4 - Hints for you, SCBCD Hints - Demnachst, SCDJWS - Auch Demnachst
Did a rm -R / to find out that I lost my entire Linux installation!
|
 |
 |
|
|
subject: which method is invoked and why?
|
|
|