Originally posted by Surya K:
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 of Base 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());
The ans is (4). But I choose (1) b'cas thought that polimorphism will take care of calling the getFields() mehtod in the subclass. I tried executing the program and the given ans (4) is correct. Something I am missing.
Pls. explain me ASAP. Planning to take exam on the weekend.
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
|