• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Marcus Green Exam 3 Q57

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you tell me in this case a is instance of Base or Agg.
Thanks.
------------------
 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Base a = new Agg(); is an instance of Agg but it is looking through the eyes of Base.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic