aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green #Exam 3 Q57 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green #Exam 3 Q57" Watch "Marcus Green #Exam 3 Q57" New topic
Author

Marcus Green #Exam 3 Q57

Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
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());
The answer is 4.
But since Agg extends Base it has access to the public method then why does 1 fail to compile. Changing the object type to Agg does give the desired answer but why does it fail for Base type. If the method is called runtime on Agg object then it would look for the instance in that object and if not found woundn't it go to the superclass for the method ? What am i missing here ??
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Soum:
This is a classic example of Polymorphism. object reference
a is of type Base but holds an Agg type object.
Polymorphism says that if you call any method on
object a, it will launch at run-time same over-
ridden method in Agg. But the catch is that
this method should also exist in Base class
as well even if it is without body. So if you
add :
public String getfields() { }
in Base class, you will get the result you
are expecting.
Thanks
Barkat
[ August 17, 2002: Message edited by: Barkat Mardhani ]
[ August 17, 2002: Message edited by: Barkat Mardhani ]
[ August 17, 2002: Message edited by: Barkat Mardhani ]
[ August 17, 2002: Message edited by: Barkat Mardhani ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Soum, the compiler will complain if the type of the reference on which the method is invoked didn't declare(1) the method.
((Base) a).getFields(); the type is Base
((Agg) a).getFields(); the type is Agg
____________
(1) declared or inherited. That is, the compiler would have accepted if Base hadn't declared getFields() but one of its superclasses had.


SCJP2. Please Indent your code using UBB Code
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Thanks Jose. I did not know that inherited method
would work also...
Barkat
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green #Exam 3 Q57
 
Similar Threads
Calling a method
Marcus Green Exam 3 Q57
Overriding Q?
marcus green mock #3 q.57
Marcus Exam #3 Question 57