aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green test 3 question 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 Green test 3 question 57" Watch "Marcus Green test 3 question 57" New topic
Author

Marcus Green test 3 question 57

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
shree vijay
Ranch Hand

Joined: Sep 18, 2000
Posts: 208
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


Regards,<BR>Shree
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Sep 18, 2000
Posts: 208
Yep, 1 as well as 4 would have been true
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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).]
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
 
 
subject: Marcus Green test 3 question 57