Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

A question on Dynamic Polymorphism

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Guys,

I am a beginner in JAVA and have a question on Dynamic Polymorphism.

Here's what my code (Base.java) looks like:



And here's the output of the above code:

********************************
Base => variable = 10
Subclass1 => variable = 20
Subclass2 => variable = 30
********************************
Base.variable = 10
Subclass1.variable = 20
Subclass2.variable = 30
********************************
####
********************************
Base => variable = 10
Sub1 => variable = 10
Sub2 => variable = 10
Sub21 => variable = 20
********************************
Base.variable = 10
Subclass1.variable = 20
Subclass2.variable = 30
Subclass2.variable = 30
********************************

As you can see, I have a Base class and a Subclass1 which inherits Base and Subclass2 which inherits Subclass1. I have a reference of Base class to the objects of Subclass1 and Subclass2.

When I invoke the method printVariable() through the references of Base class, I get the correct results, i.e. the methods of objects of respective derived classes get called at runtime, but this does NOT happen for the member variables. It always accesses the member variable of the base class, why is this so ?? Why does NOT JAVA access the member fields of the derived classes instead ??

Thanks much in advance !!!

Best Regards,
[#]
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because only methods use polymorphism/late binding.
When you reference member variables, you always reference the one that that class knows about.

In your case, your Subclasses are re-declaring the "variable". In effect, they get their own version of it, and it "hides" the version in the super class.



This example doesn't declare "variable" but does assign it a value.
It effectively inherits, and thus uses the parent classes "variable"
 
Hd Shah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You might find this useful
https://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html



Thanks Carey , this was indeed helpful !!

Keep it up !!

Cheers,
[#]
 
Hd Shah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Because only methods use polymorphism/late binding.
When you reference member variables, you always reference the one that that class knows about.

In your case, your Subclasses are re-declaring the "variable". In effect, they get their own version of it, and it "hides" the version in the super class.



This example doesn't declare "variable" but does assign it a value.
It effectively inherits, and thus uses the parent classes "variable"


Thank you Stefan , your example made it very clear for me to understand this tricky thing !!

Appreciate your prompt response , keep it up !!

Cheers,
[#]
 
Marshal
Posts: 76479
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

When you get to the class at the bottom of the inheritance hierarchy, it has two different variables of the same name, one value 20 inherited from its superclass, the other from itself value 30. You will doubtless soon work out why it is a bad idea to have several variables with the same name in subclasses, and only use that sort of code to see what happens if you …
 
Hd Shah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you Campbell !!

One quick question though, as you said I have two variables with the same name, one with the value 20 and the other with the value 30. Is there any way in JAVA where I can easily print/display the value(s) of all of the components of an object, such as all its variables and the ones derived from its superclass(es), etc. ??

Kindly advise.

Thanks again,
H.
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. You can try super.variable, but that depends on the variable being accessible (bod practice) and on there only being that one variable accessible in the superclass. You cannot write super.super.variable.

Actually I think I was mistaken. You can cast this to the type of the super‑super‑class. ((Foo)this).variable (Only available in instance methods etc)
But if that isn't bad programming, I don't know what is
 
Hd Shah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Campbell,

Sorry to have asked you stupid question, I now know it is NOT a good programming practice to have the same name variables in inheritance hierarchy as you clearly pointed out here.

Thanks much for your reply !!

Cheers,
H.
 
Campbell Ritchie
Marshal
Posts: 76479
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hd Shah wrote:. . . Sorry to have asked you stupid question, I now know it is NOT a good programming practice to have the same name variables in inheritance hierarchy as you clearly pointed out here. . . .

It is NOT a stupid question. There was something you didn't know, you asked about it, you learnt what you didn't know and you learnt something else. So it was a good question.
 
We don't have time for this. We've gotta save the moon! Or check this out:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic