File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt with instance variables and methods of the subclass and superclass

 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method if overridden in subclass then subclass version is called.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neha....
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but only if that class is a subclass....and only if the superclass has that method
 
Neha Daga
Ranch Hand
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raju Champaklal wrote:Hi Neha....


hi
All the best!!
 
dimple bav
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic