Help coderanch get a
new server
by contributing to the fundraiser

Rebecca YY

+ Follow
since Feb 08, 2001
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
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;
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.
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();
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(;
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