• 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

Doubt with instance variables and methods of the subclass and superclass

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method if overridden in subclass then subclass version is called.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha....
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:Hi Neha....



hi
All the best!!
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic