• 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

Constructors, super(), this()

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 109
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic