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?
Community
  • 1
  • 1
Bick
  • 17,833
  • 52
  • 146
  • 251
  • 4
    possibly already answered : http://stackoverflow.com/questions/1644317/java-constructor-inheritance – Eugene Dec 28 '11 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);`). – hotshot309 Nov 17 '12 at 03:54

5 Answers5

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.

Kylar
  • 8,876
  • 8
  • 41
  • 75
  • If you down vote, explain why please - I'll try to make my answer better. – Kylar Dec 28 '11 at 14:20
  • 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) – ratchet freak Dec 28 '11 at 14:28
  • Whenever you create a class, it gets a default constructor. Regardless of whether or not one exists in the super. – Kylar Dec 28 '11 at 14:34
  • 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) – Bick Dec 28 '11 at 15:13
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)

Slash
  • 496
  • 2
  • 8
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

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
  • 1
    Constructors are never inherited. The superclass's no-argument constructor will be called by the subclass's default constructor, if any. – Andy Thomas Dec 28 '11 at 15:40
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.

Lion
  • 18,729
  • 22
  • 80
  • 110
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.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108