• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors, super(), this()  RSS feed

 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is SuperDuper Class


This is Super Class



This is Sub Class


This is class Runners2


I get the output:


I have used three parameterized constructors in the class Runners2.

This changes the age value from 50 to 10.
Why does
and
doesn't change the age value to 10?

What should I do to get the output as 10 instead of 50 at line 9 & 14 in the output?
Replacing super() by this() in the Super class and Sub class also produces the same result. Why?
 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think if you set two as a Super object, the value of 10 will then show up. But since you set two and three SuperDuper objects, it will always show the age of 50.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First of all, you do know that instances of your Super class has 2 different age instance variables right? And instances of your Sub class has 3 different age instance variables?

Due to all the hiding, the constructor of the Super class and Sub class only changes the one that is declared in their respective classes. And since, your Runner2 class only displays the one that is in the SuperDuper class, your expected result is not seen.


So, how to fix? Well, it depends. If you didn't intend to have three copies of the variables, then you should fix that. And if you did intend it, but expect them to be equal, then you should fix that (by setting them all). For the latter, have the constructor call their superclass' constructors that takes an age.

Henry
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sergiu Dobozi wrote:I think if you set two as a Super object, the value of 10 will then show up. But since you set two and three SuperDuper objects, it will always show the age of 50.


Thank You so much.
Is there any way I can do it without setting two and three as Super object and Sub object respectively?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't hide your fields in sub-classes.

SuperDuper already has an age field, why are you redeclaring it in Super and Sub?

If you want to change the default value of the existing field in SuperDuper, you should do that in the default constructors of your sub-classes. Preferably, you should also make the field private and final:
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fields do not participate in polymorphism, only instance methods do. You have declared all the variables in your main() method to be of type SuperDuper, so the value of one.age, two.age, three.age are all resolved to SuperDuper.age at compile time. Try declaring variable two as type Super and variable three as type Sub.
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You, Stephan, Junilu, Henry and Sergiu. Now I've got it. Learnt more about polymorphism, method hiding and super(). Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!