• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Marcus Green test 3 question 57

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the answer given is correct however his explanation is contradictory and misleading.
Question 57)
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());
Answer to Question 57)
4) System.out.println( ((Agg) a).getFields());
The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class, which displays Base. But the call to ((Agg)a).getField() will invoke the getField() in the Agg class. You will be unlucky to get a question as complex as this on the exam.
the first two sentences are correct. sentence 3 is incorrect and contradicts sentence 2. the Base class has no method getField(). Furthermore, if it did, the method of class Agg would be called as per sentence 2.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base {
public String getFields()
{
String name="Base";
return name;}
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
}
}
This could have been the code.
During compile time the compiler is bothered only with the reference type.
Base b=new Derived();
b.myFunction();
The compiler checks whether myFunction is available in the class Base which is the type of object b. If it is not, it gives a compiler error. Else, it is happy.
But during run time, what matters is the type of the object </bold> assigned to </bold> the reference b. In this case, it is Derived object. Actually, the getFields() of agg will be executed even without the cast. I ran the following code :
</code>
class Base {
public String getFields(){
return "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();
System.out.println(a.getFields());
}
}
</code>
if there is anything wrong in my understanding, let me know.

------------------
Regards,
Shree
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if that had been the code in the question, then answer 1 would have been correct as well as answer 4(as you demonstrated in your example).
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, 1 as well as 4 would have been true
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well he did say something about being able to write him about the tests. Perhaps I should as his explanation seems wrong. The question(as written) is about accessing methods of the subclass that are new to the subclass from a superclass reference. but it seems in his explanation he got really confused.
[This message has been edited by Randall Twede (edited January 06, 2001).]
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This questions has been discussed previously, try a search here on AGG. (It still might be incorrect but see what other people have said).
Marcus
 
reply
    Bookmark Topic Watch Topic
  • New Topic