Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What use are constructors in abstract classes? [duplicate]

Since we can't instantiate an abstract class, then what is the necessity of having constructors in abstract class?

like image 935
JavaUser Avatar asked Nov 30 '25 06:11

JavaUser


2 Answers

Abstract classes are designed to be extended, each constructor from the child must perform a call to a constructor from the base class, thus you need constructors in your abstract class.

The abstract class is a skeleton and thus makes no sense to instantiate it directly since it is still incomplete (children will provide the rest).

We can use a abstract class constructor to execute code that is relevant for every subclass. This way preventing duplicate code

like image 29
Bas Avatar answered Dec 02 '25 19:12

Bas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!