• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Method Lookup

 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
reubin haz
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all

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

private instance methods looks on references
public instance methods looks on objects
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx vidya, but can somebody be more specific with my last post please.

cheers, Marzo.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx a billion Corey. You've cleared it all up for me.

thanx you all.

Cheers, Marzo!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic