Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance question (I guess)

 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am trying to extend my muscle class with spring class. so in my spring constructor I have this:

and in my muscle class I have two parameters I want to add to this constructor in the muscle construction so in my muscle class I have this:

what I am wondering is, am I supposed to write those two constructors for the muscle? Also I am wondering I didn't put a reference for the arguments in the second constructor because I didn't define them in muscle class, they are defined in the spring class, what should I do about that, can I getaway without referencing them in the constructor like above?

best
ilteris
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors aren't inherited, so each subclass must have its own.

Every subclass constructor must call the constructor of it's superclass. If you omit calling the superclass' constructor, the compiler will try to add an implicit super() call without any arguments. However, this will only work if the superclass has a no-arg constructor. Your spring class has a constructor with arguments, meaning that there is no no-arg constructor, so the compiler will not be able to call it. This means that every constructor of every subclass of the spring class must call spring's constructor through super(...), as you did in your first constructor.

Hope this helps,
Yuriy
 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the enlightening me Yury. So when I just go and instaniate the class like;


even in the class there is two constructors and the second one is like below:



you are telling me that this is going to work without implicitly adding the fields that are already in the spring class?

thanks for the patience.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ilteris kaplan:


you are telling me that this is going to work without implicitly adding the fields that are already in the spring class?

thanks for the patience.[/QB]


No, this constructor will cause a compiler error because it is not calling spring's constructor. As I said, every constructor must have a super(...) call. This call can be omitted only if the superclass has a no-argument constructor, in which case the compiler will simply added an implicit super() call for you.
 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh I thought every class needs a constructor that has a super call in that constructor only. returning to my example then I don't necessarily need that first constructor there right?

instead of that, I can just go ahead and have this constructor below:



and this will work if I have 2 fields that are amplitude and phase in my muscle class.

am I right now?

thanks!!!
[ April 21, 2006: Message edited by: ilteris kaplan ]
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that should work.

Cheers,
Yuriy
 
ilteris kaplan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your kind explanation yuriy.

now back to work!

best
ilteris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic