wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus q# 57 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus q# 57" Watch "Marcus q# 57" New topic
Author

Marcus q# 57

Ketuman Joshi
Greenhorn

Joined: Dec 19, 2000
Posts: 9
Hi Guys,
Can anyone explain me why answer is 4 & not 1& 4.
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());
Thanks
Ketuman Joshi
quan zhu
Greenhorn

Joined: Dec 14, 2000
Posts: 27
object a isA type Base, which does not have a method called "getFields".
Since a is really a "Egg" object, we can cast it.
Ken Pullin
Ranch Hand

Joined: Jan 29, 2001
Posts: 43
1 is not correct because the base class doesn't have a getFields() method.
Alamu Vinai
Greenhorn

Joined: Feb 01, 2001
Posts: 19
The Base class does not have getFields() method.
The compiler just looks for the base class variable and the base class methods, in this case we don't have getFields() method in base class and hence compiler error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus q# 57