Constructor Inheritance

During interview or anyway, many people ask constructor can be inherited or not?

We normally says 'no'. But many people says 'yes'.

Both answers are true. But it is necessary to know in which situation constructor can be inherited or not?

Constructor can be inherited:

  1. When both classes base and derived have same type of constructor such as default-default,parameterized-parameterized.

  2. When derived has default and base has parameterized.

Cnstructor can not be inherited:

  1. When base class has parameterized constructor and derived class has default constructor.

It should also know that if both base and derived class have default constructor then base class constructor will be executed first.BUT in case of static constructor derived static constructor will be executed first and after that base static constructor.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now