File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata 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
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Marcus Green Exam 3 Q57" Watch "Marcus Green Exam 3 Q57" New topic
Author

Marcus Green Exam 3 Q57

Rebecca YY
Greenhorn

Joined: Feb 08, 2001
Posts: 2
Hi there,
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());

Q:method invoked depending on object, so here a.getFields() seems should invokes getFields() in the Agg class, why Base?
Lokesh Mahajan
Greenhorn

Joined: Feb 01, 2001
Posts: 22
Dear
Just go through the following modified example.
class Base {
public String getFields(){
String name1 = "base";
return name1;
}
}
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();
System.out.println(a.getFields());
//System.out.println( ((Agg) a).getFields());
}
}
out put will be:Agg.
What I think is that during run time late binding will take place here in above modified example & it will print "Agg",because during run time it finds out that it is a object of class Agg.
but in your example it is not so.object "a" is of "type" class Base, so when it does not find method there,during COMPILATION TIME so it throws a error.
but when we type cast it back to Agg class it works giving an output"Agg"
correct me if i am wrong
love
lokesh
Rebecca YY
Greenhorn

Joined: Feb 08, 2001
Posts: 2
Thanks Lokesh,
So you mean the dynamic look-up happens during the runtime, and only when the reference find the method in its 'own' class(the reference type class), can it compile correctly. Then all these are--there is actually no overriding from the beginning, so...
Then the ((Agg)a).getFields() is actually a combination of
Agg b=(Agg)a;
b.getFields();
in which b will find its method in compile time.
Is that right? Thanks a lot. When things get complicated a little, we often forget the very basic.
Rebecca
Kevin Cary
Ranch Hand

Joined: Jan 24, 2001
Posts: 53
I agree. When the reference is constructed using "Base a = new Agg();" the default is to search the superclass (parents exsist before their children.) It seems that the subclass is seached for overriding methods or when the reference is recast telling the JVM that you want the subclass searched.
mahesh jhamtani
Greenhorn

Joined: Jan 12, 2001
Posts: 11
Can you tell me in this case a is instance of Base or Agg.
Thanks.
------------------
Kevin Cary
Ranch Hand

Joined: Jan 24, 2001
Posts: 53
Base a = new Agg(); is an instance of Agg but it is looking through the eyes of Base.
Zafar Iqbal
Greenhorn

Joined: Feb 27, 2001
Posts: 1
Hi people.
sorry to disturb you.
I has seen what you have posted. Lastly one of u asked that is
a is an instanceof Agg or Base.
sure it is instanceof both. but as the base class is matched first so it will not provide the related function. if you have dereferenced the *a* to Agg by casting then it will give you the function of Agg, unless you have not overridden the Base class methods. If Base class had defined or declared any of these method then it would have been possible to call in this manner.
a.getField()
but in your scenario you must do it in the following way.
((Agg)a).getField()
Thanx
And inform me if I am wrong.

ziqbalbh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green Exam 3 Q57