What is the use of creating a constructor for an abstract class in Java?

I would like to know the purpose of the constructor for an abstract class; since we are not creating abstract classes, why do we need such a constructor?

+7
java constructor abstract-class
source share
8 answers

there will be times when you have the usual initialization of instance variables that all inheriting classes need to set. You execute an instance of an abstract class when you extend it, and that a particular class has a constructor that will either provide parameters to the constructor of the abstract class.

+9
source share

They can still be called by class constructors that inherit from this, making code refactoring useful for using the constructor in an abstract class.

+5
source share

If you have uninitialized trailing fields in an abstract class, you will need to initialize them in the constructor.

eg.

abstract class A { final int x; } 

will not compile without a constructor that assigns x .

+3
source share

If your class does not declare a constructor, javac will create a no-arg, do-nothing constructor for you. Then, when your subclass is initialized, it will call the generated no-op constructor, and life will be good.

However, if your class declares any constructor, javac will NOT make it for you. In this case, the constructor of the subclass must explicitly call the constructor of the parent class. Otherwise, you cannot initialize the members of the parent class, as mentioned above.

+3
source share

Constructors for abstract classes are used by subclasses (called from subclass constructors using super(params) ).

You must make these protected constructors to make this clear.

+2
source share

You do not instantiate abstract classes, but the constructor is called when you instantiate the subclass.

The use may consist in initializing common attributes, i.e.

 import java.util.List; import java.util.ArrayList; abstract class BaseClass { protected List list; // visible from subclasses public BaseClass() { System.out.println("to abstract..."); // common initialization to all subclasses list = new ArrayList(); list.add("a"); list.add("a"); list.add("a"); } } class ConcreteClass extends BaseClass { public ConcreteClass(){ // The list is initialized already System.out.println("now it is concrete and the list is: = "+ this.list ); } } class TestAbstractClass { public static void main( String [] args ) { BaseClass instance = new ConcreteClass(); } } 

Exit

 $ java TestAbstractClass to abstract... now it is concrete and the list is: = [a, a, a] 
+2
source share

De-duplication of general knowledge / behavior.

eg. Car: all your cars will be built from a body and four wheels and an engine. Thus, you execute this part of the construction in the constructor for the abstract Car class, calling functions like Body (), Wheel (int x), Engine (). Each particular class of cars will have its own implementation of Body (), Wheel () and Engine (), but they will all take the same steps to build a car with them, so there is no need to duplicate these steps in each of these classes. In this case, you implement this usual behavior in the ancestor.

+1
source share

I agree, the designers are created under the assumption that there will be instances. If you have a lot of common code, you might consider creating a constructor, but it's much better to put it in the init() method.

-one
source share

All Articles