File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dynamic Method Lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dynamic Method Lookup" Watch "Dynamic Method Lookup" New topic
Author

Dynamic Method Lookup

Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Hi ppl, the code seems long but is quite simple.
My dought is explained below..


public class Relo{
public static void main(String[] args){
A ref1 = new C();
B ref2 = (B) ref1;
System.out.println(ref2.g());
}
}

class A{
private int f(){
return 0;
}
public int g(){
return 3;
}
}

class B extends A{
private int f(){
return 1;
}
public int g(){
return f();
}
}

class C extends B{
public int f(){
return 2;
}
}


Why is it that the g() method in class B is called when the actual object type at runtime is of type C. Doesn't C inherit public int g() from class B?
I thought it would of called the inherited g() method of the C class.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
I'm not sure whether i understood your question correctly.
But the output is 1 because.

A ref1 = new C();
B ref2 = (B) ref1;

ref2 is actually an object of 'C' so ref2.g() will search for the method g() in class 'C' & since Class 'C' has not overrided g() the method g() in Class 'B' ( it's Parent ) is called.
[ June 21, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Okay - let me answer a question with a question...

What happens if you change the method B.f() from private to public? What's your output?

Now, with that in mind, can you answer your own question?


SCJP Tipline, etc.
reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Originally posted by Corey McGlone:
Okay - let me answer a question with a question...

What happens if you change the method B.f() from private to public? What's your output?

Now, with that in mind, can you answer your own question?


Hi Corey, I tried your idea, and it invokes the method in C. I'm wondering why having an explicit casting to a parent class does not affect the method it calls. Could you explain more? Thanks.


SCJA, SCJP5.0, SCBCD, SCWCD
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by reubin nibuer:
Could you explain more? Thanks.


How are B.f() and C.f() related?


Are you sure about that answer?





Here's a hint - C.f() does not override B.f(). Why not? Knowing that, does the original output make sense?
Shivakanth Thyagarajan
Ranch Hand

Joined: Mar 28, 2005
Posts: 41
Hi Corey,
Isn't it true that private methods cannot be over-ridden in the subclass. I believe you are pointing you hint in that direction.

Thanks
Shiva
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

can someone please explain this to me. i understand that the g() method in in the B class is called because, although the C class inherits the g() method, it does not override it? is this correct ppl?

so therefor once in the g() method of the B class it will call its own private f() method, but wouldn't a 'this' reference be passed to the B class pointing back to the C class, so it can use it's own f() method (in the C class that is)?
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
hi all

in simple words............

private instance methods looks on references
public instance methods looks on objects
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

thanx vidya, but can somebody be more specific with my last post please.

cheers, Marzo.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Marzo,

Sorry if I was being unclear - I was hoping to lead you to the answer on your own, rather than simply spelling it out. You know, the whole "teach a man to fish" bit.

Anyway, the g() method is properly overridden from A to B. C, of course, defines no g() method so, when you invoke that method on a C instance, B.g() in invoked due to inheritance.

Now, the tricky part here is the relationship between the method B.f() and C.f(). Notice the access modifier on f() in each of those classes. In B, f() is defined as a private method! That means that you can't override it because f() isn't inherited from B to C. With no overriding, there's no polymorphism. That means that, from B.g(), we're not going to do any sort of dynamic method lookup - we're just going to invoke B.f() and be done with it.

That's why my first suggestion, changing B.f() from private to public, would result in C.f() be invoked. By changing that method to be public, the method is overridden in C and we perform dynamic method lookup, invoking C.f().

It's hidden my some extra garbage, but the real trick to this question is to know that private methods can not be overridden.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

You know, the whole "teach a man to fish" bit.


If you teach a man to build a fire, he'll be warm for a night. If you set a man on fire...


Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

thanx a billion Corey. You've cleared it all up for me.

thanx you all.

Cheers, Marzo!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Method Lookup
 
Similar Threads
Overridding private methods
out put of class
Polymorphism
Polimorphism x Casting
Inheritance doubt