File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus q# 57" Watch "Marcus q# 57" New topic

Marcus q# 57

Ketuman Joshi

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();
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());
Ketuman Joshi
quan zhu

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

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.
jQuery in Action, 3rd edition
subject: Marcus q# 57
It's not a secret anymore!