aspose file tools
The moose likes Java in General and the fly likes Extending a class; Must I explicitely call the super constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Extending a class; Must I explicitely call the super constructor?" Watch "Extending a class; Must I explicitely call the super constructor?" New topic
Author

Extending a class; Must I explicitely call the super constructor?

Kentaro Shinbashi
Greenhorn

Joined: Jun 08, 2006
Posts: 15
Hello.

I'm extending the JSlider class:
class MySlider extends JSlider{....}

I would like to do something like:
new MySlider(0, 10)

To make a slider from 0 to 10.

Aparently, the constructor JSlider(int, int) is not inherited automatically.

Do I have to make a constructor like:



Am I missing something?

Thanks.

Kentaro
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
Constructors are never inherited.

If a subclass constructor does not have an explicit call to a constructor of its direct superclass, then an attempt is made to call a constructor from the direct superclass which has no parameter list.
[ June 24, 2006: Message edited by: Keith Lynn ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
If you subclass a class that does not have a no-argument constructor, you must also specify an explicit constructor of the subtype (with or without parameters). Let's look at a simpler example:

Class C meets the criteria above (does not have a no-argument constructor). Let's see what happens if you subclass it.

Compile-time error. Class T contains a default constructor, which, since it has no explicit call to super(...) or this(...), an explicit call to super() is made (this holds for all default constructors). Let's look at class T again, but with the implicit default constructor specified explicitly.

Note now that there is an explicit call to super(). The previous declaration of T also has this call, however, it is implicit. Note also that the constructor "super()" does not exist. Simply, there is not a no-argument constructor for type C (the superclass). This is why compilation fails. You should see an error message relating to the constructor invocation where the constructor does not exist.

The following declaration will pass compilation.

So will this declaration.

[ June 24, 2006: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Kentaro Shinbashi
Greenhorn

Joined: Jun 08, 2006
Posts: 15
Thank you, Tony and Keith.

So, if I subclass JSlider, and I want to use all of the constructors of JSlider, then I must implement them all with super.

Thanks for your help.

Kentaro
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extending a class; Must I explicitely call the super constructor?
 
Similar Threads
simple slider question
Have I got my inner classes and variable declarations in the right places?
Movie-like timeline with JSlider
Extending JSlider
JPanel question