aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with instance variables and methods of the subclass and superclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with instance variables and methods of the subclass and superclass" Watch "Doubt with instance variables and methods of the subclass and superclass" New topic
Author

Doubt with instance variables and methods of the subclass and superclass

dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
HI,

I am not able to get this code: Chapter 2 K&B exercise problem 13
class Mammal
{
String name = "Hello";

String make()
{
return "A";
}
}


class zef extends Mammal
{
String name = "HHH";

String make()
{
return "S";
}
}

public class Zoo
{
public static void main(String args[])
{
new Zoo().go();
}

void go()
{
Mammal m = new zef();

System.out.println(m.name+m.make());
}
}


Output: HelloS

I know m is an object of class zef referencing to class Mammal.

So m.name will display the instance variable of class Mammal.

But what about the method make ?
Why the instance method looks the subclass.

Can anyone explain this ?

Thank you.
Dimple.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
which overridden method to chose depends on the object at runtime...and which variable to chose depends on the reference variable at the compile time....


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Raju Champaklal wrote:which overridden method to chose depends on the object at runtime...and which variable to chose depends on the reference variable at the compile time....


Variable is related to the reference Mammal
Method is related ?
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
method if overridden in subclass then subclass version is called.


SCJP 1.6 96%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
Hi Neha....
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Raju Champaklal wrote:which overridden method to chose depends on the object at runtime...and which variable to chose depends on the reference variable at the compile time....


i didnt got your statement.

Can you explain ?

Thank you.
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Neha Daga wrote:method if overridden in subclass then subclass version is called.


Hi

I got it very well from the example of K&B Polymorphism in Overloaded and Overridden methods (Chp 2)

But it means variable m will always go the Class to which it is referencing ?


Thank you.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
but only if that class is a subclass....and only if the superclass has that method
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
when a reference variable of a class refers to subclass object then it knows only about the methods in super class but if any of the super class's method is overridden by the subclass then overridden version is called this is the only thing the actual object has to do with method calls, and about instance variable, reference variable has visibility only to super class variable that is whose instance has been created and not the subclass variable whose object is being referred.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Raju Champaklal wrote:Hi Neha....


hi
All the best!!
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Neha Daga wrote:when a reference variable of a class refers to subclass object then it knows only about the methods in super class but if any of the super class's method is overridden by the subclass then overridden version is called this is the only thing the actual object has to do with method calls, and about instance variable, reference variable has visibility only to super class variable that is whose instance has been created and not the subclass variable whose object is being referred.


Hey thanks Neha.

I got it your explaination very well.

I wouldnt be able to go further if my doubts were not cleared at this stage.

Thank you.
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
Hi Neha

I am referring K&B.

I know this book is enough . I have to master the book .

Do you advice me anything ?

Thank you.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I am following just the K & B and that is a great book you just need to master it and then go for mock exams.

that's what I am doing.
 
Consider Paul's rocket mass heater.
 
subject: Doubt with instance variables and methods of the subclass and superclass