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:
- When both classes base and derived have same type of constructor such as default-default,parameterized-parameterized.
- When derived has default and base has parameterized.
Cnstructor can not be inherited:
- 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.