Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Excellent, intuitive constructor exam q, !.. help!

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
David Harrigan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Hey cool! They got a blimp! But I have a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic