0

Possible Duplicate:
Java Constructor Inheritance

When creating subclasses,

  1. Why do I have to create a constructor and write super in every ctor?
  2. If my behavior is similiar to the superclass shouldnt I expect to inherit them as well?
2
  • 4
    possibly already answered : stackoverflow.com/questions/1644317/… Commented Dec 28, 2011 at 14:13
  • You don't have to write in super in every constructor, necessarily. Assume the superclass has a default (public no-argument) constructor. The subclass will, if you specify no constructors in it, implicitly get a default constructor that simply calls super(); at compile time. Any constructor you explicitly create will have an implicit call to super(); as its first line--unless in the first line of the constructor you explicitly call one of the overloaded constructors in the superclass (for instance, super(myParameter);). Commented Nov 17, 2012 at 3:54

5 Answers 5

1

Constructors aren't inherited because you're not creating an instance of the superclass, you're creating an instance of a new class. There's no way to know which superclass constructor you want to call.

To be fair, the default (no arg constructor) always exists. It's the specific arg ctors that you're referring to, I'm assuming.

Sign up to request clarification or add additional context in comments.

4 Comments

If you down vote, explain why please - I'll try to make my answer better.
the default only exists when the default in the super exists in other words if all the super constructors all need args then the default constructor is not created (or generates compile errors)
Whenever you create a class, it gets a default constructor. Regardless of whether or not one exists in the super.
Hi, What's strange to me is that if lets say I have 3 Ctors in the super class. inheriting only one is compiler-correct. Now, I would expected that if I didnt inherit a ctor it would still be ok to use when creating the the sub class. like when using one of it's methods. I see no problem with that. the jvm will know which ctor i use because it is in the super class (BTW:Didn't downvote)
0

In reality, java always creates new constructors for your subclasses, but only for the default, no parameters constructor. If your class has constructors other than the default (no parameters) constructor, you have to define them again... Constructors are not meant to be behaviour methods but rather object initialization (which may change for subclasses with new attributes)

1 Comment

"Always" is incorrect. A default constructor is provided for classes that have no constructor declarations.
0

Default no argument Constructor is inherited and called by default. In case you like to call another one, you can use super() . In case default constructor can not be used, you have to use one4 of accessible super constructors

1 Comment

Constructors are never inherited. The superclass's no-argument constructor will be called by the subclass's default constructor, if any.
0

Constructors have the same name as the class name and if you're able to inherit them into the subclasses, they can no longer be constructors of subclasses. The default parameterless constructor is always inherited though.

1 Comment

Constructors are never inherited. The default constructor calls the superclass's no-argument constructor.
0

Constructors are invoked to create objects from the class blueprint, ie, to initialise the data members of a class. If a subclass were to inherit a constructor,then while calling a subclass you need to have the knowledge about the parent class' data members too. That is not what one would see in real life scenario. For example, if you create an object of type Ferrari, you would definitely be interested about initialising parameters like speed,acceleration and you would not bother about initialising the parameters of a Car object, even though Ferrari inherits from Car. Therefore, while calling the constructor of a Ferrari, you would only and only be bothered about the members of Ferrari and not the members of its parent class. I hope to have made my point clear.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.