• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime

 
Om Sonie
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that once you create an object, it always calls the method associated with the object and not the type of reference (e.g. parent).
But following question has confused me.
Why it says ans C is wrong? The object a is still child so even if you cast it Base, it should still call object method.
Can anyone explain. Thanks.
-----------------------------------------------
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());
Ans: 4) System.out.println( ((Agg) a).getFields());
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Om Sonie:
My understanding is that once you create an object, it always calls the method associated with the object and not the type of reference (e.g. parent).
But following question has confused me.
Why it says ans C is wrong? The object a is still child so even if you cast it Base, it should still call object method.
Can anyone explain. Thanks.

Hi Om
Your understanding is correct but the option 3 is casting a string to type Base object which is wrong and you will get compiler error as "invalid cast". SO it should be ((Base) a).getFields()), then this answer is correct.
Even option 1 is correct. so correct answers are 1 and 4
Correct me if I am wrong.
Thanks
Jyotsna
 
Daniel Wu
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jyotsna Umesh:
Hi Om
Even option 1 is correct. so correct answers are 1 and 4
Correct me if I am wrong.
Thanks
Jyotsna

I think option 1 is wrong because
a is a object of type base which has no getfields method; the getfields method is unique to Agg. So a needs to be casted back to its original type Agg before invoking the getfields method. Right?
 
Om Sonie
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,
Yes 1 does not compile.
But still i am confused. Is "a" Base object or Agg object. I think it is Agg object. It is stored in reference Base. Am I right?
Then when the method is called, that should be called based on object "a" and not based on the reference Base?
Sorry to drag it!!
Om
 
V Srinivasan
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Daniel, Your thinking is right, Option 1 is wrong in this situation. But I want to clarify you that a is reference type of Base and object type of Agg, i.e. reference a has initialised with object Agg, OK. In relational term, super class variable holds sub class's object.
What happens, in case of super class's method overridden by sub class, which is called, then it does't need for casting, simply it will execute the sub class's method. But in case super class does't have a method which sub class holds, then casting needed. Though it is an object of sub class, it first checks super class(because it is a variable of super class), if it finds satisfies and executes the overridden method in sub class because it is initialised with sub class. Interestingly, if the sub class overrides the super class method, particularly in this type of situation we cannot call the super class method. Please anybody clarify why this happening.
Thanks and regards,
V. Srinivasan
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
1) Wrong. The compiler only knows the declared type 'Base' which doesn't have a getFields() method.
2) Wrong. Same as above, compiler only knows the declared type and 'Base' does not have a field 'name'
3) Wrong. Casting to 'Base' type doesn't help as 'Base' does not have the getFields() method.
4) Right. The cast tells the compiler that the real type of 'a' is 'Agg'. It knows that type 'Agg' has a getFields() method so the compile works ok. At runtime, 'a' really is a type 'Agg' (as stated by the cast) and so there is no runtime error.
The compiler only knows what the declared type of an object. If a called method or referenced field does not exist in the declared type you will get a compiler error.
A cast allows you to tell the compiler that things are ok, the referenced object may be declared as one type but is actually of another type. The compiler takes your word for it and doesn't raise an error. However, at runtime the JVM knows the real type of the object. If 'a' in the above example had not really been an 'Agg' type a runtime error would be raised.
Just an FYI, you can't cast answer (2) to 'Agg' in the same manner. Both the runtime and the compiler use the declared type for fields.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane
Would this be a fair summary of what you said?
In this case with the code given it is not an override situation so the compiler just uses the type of the reference, not the underlying object.
I ran test and added a getFields() methods to the Base class and now line 1 'System.out.println(a.getFields());' works. this is because it is now overriding the Base class method and the compiler can use late binding?
Please let me know if I'm way off on this

Thanks
Dave
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Vick:
Jane
Would this be a fair summary of what you said?
In this case with the code given it is not an override situation so the compiler just uses the type of the reference, not the underlying object.
I ran test and added a getFields() methods to the Base class and now line 1 'System.out.println(a.getFields());' works. this is because it is now overriding the Base class method and the compiler can use late binding?
Please let me know if I'm way off on this

Thanks
Dave


Hi Dave,
I agree with you, Actually I too tried adding getFields() method in class Base and becoz of late binding the option 1 seems to be correct so in this case option 1 and 4 ar right provided there is this method in Base class.
Jyotsna
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic