Help coderanch get a
new server
by contributing to the fundraiser

Rebecca YY

Greenhorn
+ Follow
since Feb 08, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rebecca YY

Thanks Lokesh,
So you mean the dynamic look-up happens during the runtime, and only when the reference find the method in its 'own' class(the reference type class), can it compile correctly. Then all these are--there is actually no overriding from the beginning, so...
Then the ((Agg)a).getFields() is actually a combination of
Agg b=(Agg)a;
b.getFields();
in which b will find its method in compile time.
Is that right? Thanks a lot. When things get complicated a little, we often forget the very basic.
Rebecca
23 years ago
Hi there,
class Base {}
class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the
output of the string "Agg"?
1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());

Q:method invoked depending on object, so here a.getFields() seems should invokes getFields() in the Agg class, why Base?
23 years ago