• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question on Dynamic Polymorphism  RSS feed

 
Hd Shah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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,
[#]
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find this useful
https://docs.oracle.com/javase/tutorial/java/IandI/hidevariables.html
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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,
[#]
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56578
172
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!