the answer given is correct however his explanation is contradictory and misleading.
Question 57)
Given the following code
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());
Answer to Question 57)
4) System.out.println( ((Agg) a).getFields());
The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class, which displays Base. But the call to ((Agg)a).getField() will invoke the getField() in the Agg class. You will be unlucky to get a question as complex as this on the exam.
the first two sentences are correct. sentence 3 is incorrect and contradicts sentence 2. the Base class has no method getField(). Furthermore, if it did, the method of class Agg would be called as per sentence 2.