Are constructors inherited? Can a subclass call the parent's class constructor? When?

You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.

Showing Answers 1 - 2 of 2 Answers

ishita

  • Jan 3rd, 2006
 

Constructors are not inherited. In other words, just because a base class has a constructor taking a certain list of parameters doesn't mean that a derived class has a constructor taking that list of parameters. (It can, by providing one itself, but it doesn't inherit it from the base class.)

  Was this answer useful?  Yes

manjunath shenoy

  • Jan 18th, 2006
 

no,the constructors cannot be inherited.the main reason for that is that we dont want to override the superclass constructor.Yes, asubclass can call its parent's class constructor.This is possible by using SUPER keyword in java.Through this keyword we can call the constructors of our choice(of parent class).egclass abc{abc(){}abc(int a){System.out.println(a+");}}class def extends abc{def(){super(10);System.out.println("hi");}}o/p 10 hi//this o/p will be generated when v create instances of class def.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.