Can a abstract class have a constructor ?When would the constructor in the abstract class be called ?

All the classes including the abstract classes can have constructors.Abstract class constructors will be called when its concrete subclass will be instantiated.

Questions by devs

Showing Answers 1 - 12 of 12 Answers

Amar

  • Dec 19th, 2006
 

constructor can not be created in an abstract class.even though we write the constructor, when the constructor will be called..?only if the object is created. but we cannot create an object of abstract class.even though we create object using indirect way, there also, we cannot call the constructor of the abstract class.finally, constructor for the abstract class cannot be called.

  Was this answer useful?  Yes

Sonal A. Watane

  • Dec 26th, 2006
 

yes, we have constructor in abstract class.An abstract class may have constructors to initialize methods or pass values along to base class constructors.

  Was this answer useful?  Yes

Yes, Abstract Class can have Constructor, infact I would say it SHOULD have constructor.

As ABS class is not a class WITH NO FUNCTIONALITY, it always can HAVE some common functionality.

Please understand that ABS class is meant for GENERALISATION, like ANIMAL Class, PLANT Class, LIQUID class etc.
So, an ABS class can have Constructor, which will be called when an object of its DERIVED class will be instantiated.

(There is a LITTLE difference between an ABS class and an Interface, but YES, there is).

  Was this answer useful?  Yes

Amms

  • Sep 30th, 2011
 

Abstract class constructor can be called from the subclass that is extending this abstract class.

You would define a constructor in an abstract class if you are in one of these situations:

•you want to perform some initialization (to fields of the abstract class) before the instantiation of a subclass actually takes place
•you have defined final fields in the abstract class but you did not initialize them in the declaration itself; in this case, you MUST have a constructor to initialize these fields

  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.

 

Related Answered Questions

 

Related Open Questions