This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes inheritance question (I guess) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance question (I guess)" Watch "inheritance question (I guess)" New topic
Author

inheritance question (I guess)

ilteris kaplan
Ranch Hand

Joined: Jan 21, 2006
Posts: 38
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

Joined: Dec 13, 2004
Posts: 429
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

Joined: Jan 21, 2006
Posts: 38
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

Joined: Dec 13, 2004
Posts: 429
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

Joined: Jan 21, 2006
Posts: 38
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

Joined: Dec 13, 2004
Posts: 429
Yup, that should work.

Cheers,
Yuriy
ilteris kaplan
Ranch Hand

Joined: Jan 21, 2006
Posts: 38
thank you for your kind explanation yuriy.

now back to work!

best
ilteris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inheritance question (I guess)
 
Similar Threads
another newbie needs help...:-(
constructors
switch statement
getting a java.lang.NoSuchMethodError: main
Overload methods in subclass?