This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

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.
I agree. Here's the link:
subject: Marcus q# 57
jQuery in Action, 3rd edition