This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes which method is invoked and why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "which method is invoked and why?" Watch "which method is invoked and why?" New topic
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
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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 - Every great individual common man
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..
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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: 9399
    
    2

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, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which method is invoked and why?