• 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

Please, Please, Help . . . URGENT

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
What code placed after the comment //Here will result in calling the getFields method of Base 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());
The ans is (4). But I choose (1) b'cas thought that polimorphism will take care of calling the getFields() mehtod in the subclass. I tried executing the program and the given ans (4) is correct. Something I am missing.
Pls. explain me ASAP. Planning to take exam on the weekend.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, there:
Let me try to help.if anything wrong, correct me.
-----------------------------------------
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();
}
}
What code placed after the comment //Here will result in calling the getFields method of Base 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());
The ans is (4). But I choose (1) b'cas thought that polimorphism will take care of calling the getFields() mehtod in the subclass. I tried executing the program and the given ans (4) is correct. Something I am missing.
Pls. explain me ASAP. Planning to take exam on the weekend.
--------------------------------------
The method that gets invoked depends on teh actual type of the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class, which display Base.
But the ((Agg)a).getField() will invoke the getField() in the Agg class.
hope this help.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Surya K:
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();
}
}
What code placed after the comment //Here will result in calling the getFields method of Base 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());
The ans is (4). But I choose (1) b'cas thought that polimorphism will take care of calling the getFields() mehtod in the subclass. I tried executing the program and the given ans (4) is correct. Something I am missing.
Pls. explain me ASAP. Planning to take exam on the weekend.


____________________________________________________
Hi
a.getFields() invokes getFields() method of Agg only if there
is a similar method in Base also ,since method overriding
is done in Agg class.Here,getFields() method is a normal instance method of Agg class i.e it is not a overridden method of Base.So, in order to invoke this method,
u have to first cast a to the class Agg and then getFields
can be invoked.Hope i am clear.
ThankYou

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
---------------------
The method that gets invoked depends on teh actual type of the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class, which display Base.
But the ((Agg)a).getField() will invoke the getField() in the Agg class.
hope this help.
------------------------
Actually not always. If the Base class have a method with the same name then only the polimorphism will work and always for this situation the method from subclass will be invoked. What I meant to say is
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[]){
Agg b = new Agg();
Base a = b;
System.out.println( ((Agg) a).getFields());
System.out.println( a.getFields());
}
}
In the above situation always "Agg" will be printed. B'cas polimorphism will take place. In our first prog. We did not have the same method in base class so we can not call just like a.getField(). Now I am clear.
hope you also understand the same.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Helen,
I don't agree with you. The explicit cast is required because there is no method called getFields in class Base. So if you try compiling it without a cast it would result in a compiler error saying that the method not found. Polymorphism works only when the methods are overridden and not for newly defined methods.
Correct me if I am wrong.
Uvnik
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi avn,
I got it. Thanks for your reply.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do agree with uvnik.
I would like to add one more point over here may be helpful to others
see what happens if we make getFields() as a "static" methods, even though getFields() is overriden we get output as follows after executing Avf, careful with static.
output
-----
Agg
Base

-------
class Base {
static public String getFields(){
String name = "Base";
return name;
}
}
class Agg extends Base{
static public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Agg b = new Agg();
Base a = b;
System.out.println( ((Agg) a).getFields());
System.out.println( a.getFields());
}}
 
Helen Yu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Uvnik and avn:
You are right! Thanks. -
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'd like to add something to what siva said...
--------------------
see what happens if we make getFields() as a "static" methods, even though getFields() is overriden we get output as follows after executing Avf, careful with static.
output
-----
Agg
Base

-------
class Base {
static public String getFields(){
String name = "Base";
return name;
}
}
class Agg extends Base{
static public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Agg b = new Agg();
Base a = b;
System.out.println( ((Agg) a).getFields());
System.out.println( a.getFields());
}}
---------------------
ploymorphism for overriding methods clearly states that - " it is the type of object being referred to and not the reference variable which determines which object will be called".
static methods/ variables exist as a single copy for a class and not for individual objects of the class. So the above rule may not really be applicable.
We know that when static methods of a different class are called in another static method (say main) we call them with the syntax : classname.methodname
So maybe here the interpretation of :
System.out.println( ((Agg) a).getFields());
System.out.println( a.getFields());
is Agg.getFields();
and Base.getFields();
What do u guys think...... does this make sense !!
eskay
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic