Excellent, intuitive constructor exam q, !.. help!
sarim raza
Ranch Hand
Joined: Nov 02, 2000
Posts: 232
posted
0
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()); // I CHOSE 1 SINCE a (the way it is constructed) is supposed to access the methods of Agg , right? then why is it wrong ?
David Harrigan
Ranch Hand
Joined: Dec 12, 2000
Posts: 52
posted
0
Hiya! It's to do with polymorphism. The simple rule to remember is that if a variable is created using this syntax:- Base v = new Child(); Then the *methods* of the base will be called if you do something like:- v.aMethod(); whereas the variables of the child will be called if you something like this:- v.aVariable; You are asked this in the exam... If you want to get access to the getFields method, you have to cast the a to be of type child:- ((Agg)a).getFields(); So the correct answer is (4). David.
Tom Chen
Ranch Hand
Joined: Dec 13, 2000
Posts: 46
posted
0
Hi, David I don't agree with your rule.It seems that the rule should be reversed. As a matter of fact, in this question the Base class has no getFields() method ,so dynamic binding doesn't apply to calling this method.Instead, we should do some casting in order to call the method. So correct answer is 4.
Originally posted by David Harrigan: Hiya! It's to do with polymorphism. The simple rule to remember is that if a variable is created using this syntax:- Base v = new Child(); Then the *methods* of the base will be called if you do something like:- v.aMethod(); whereas the variables of the child will be called if you something like this:- v.aVariable; You are asked this in the exam... If you want to get access to the getFields method, you have to cast the a to be of type child:- ((Agg)a).getFields(); So the correct answer is (4). David.
[This message has been edited by Tom Chen (edited December 18, 2000).]
Sun Certified Java Programmer
David Harrigan
Ranch Hand
Joined: Dec 12, 2000
Posts: 52
posted
0
Tom, I agree with your answer, that is why I have indicated in my response, that a type-cast is required because Base does not have the getFields() method. I agree the answer should be 4. However, the variable was declared as being of type "Base" and since Agg extends Base then RTTI does indeed occur... David.
Sivaram Ghorakavi
Ranch Hand
Joined: Nov 30, 2000
Posts: 56
posted
0
David's explanation is true only if the super class does not have any method defined. If's is over loading... in subclass then Base v = new Child(); v.aMethod(); would execute the child's over loaded method. And v.aVeriable would call the Base aVariable.
Ajith Kallambella
Sheriff
Joined: Mar 17, 2000
Posts: 5782
posted
0
Consider the statement Base a = new Agg();
Eventhough you are instantiating an object of class Agg, due to reference assignment to type 'a', compiler checks to see if the method invocation( or variable reference ) is valid with regard to class Base. By assigning the new 'Agg' object to type 'a', you are actually pointing to the 'Base' class instance contained in the derived object of type 'Agg'. This means if you need to access any of the attributes/methods of the derived class, you will have to re-cast the object to its original type, ie., 'Agg'. Polymorphism is a runtime behaviour which can be observed if the actual type of object being pointed to overrides any method in the base class, in which case the newer version of the method gets executed.
Sivaram, you said -
Base v = new Child(); v.aMethod(); would execute the child's over loaded method. And v.aVeriable would call the Base aVariable.
( Please see the text in bold. ) I think you meant overridden method and not overloaded method. Please make sure you understand the difference between the two. Overloaded methods are actually different methods that only share the common name. Unlike overridden methods, overloaded methods do not exhibit "true" polymorphism. You can override a method only once, whereas you can overload a method any number of times with different signatures. Ajith
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Excellent, intuitive constructor exam q, !.. help!