62

Why does an abstract class in Java have a constructor?

What is it constructing, as we can't instantiate an abstract class?

Any thoughts?

N J
  • 25,967
  • 13
  • 73
  • 94
gameover
  • 10,862
  • 16
  • 55
  • 69
  • 1
    check out this question for clarification, http://stackoverflow.com/questions/260666/abstract-class-constructor-in-java – Anthony Forloney Jan 31 '10 at 04:00
  • You need a way to construct sub-class of an abstract class. Technically, a public constructor on an abstract class is really just a protected constructor. – Peter Lawrey Jan 31 '10 at 09:33

7 Answers7

94

A constructor in Java doesn't actually "build" the object, it is used to initialize fields.

Imagine that your abstract class has fields x and y, and that you always want them to be initialized in a certain way, no matter what actual concrete subclass is eventually created. So you create a constructor and initialize these fields.

Now, if you have two different subclasses of your abstract class, when you instantiate them their constructors will be called, and then the parent constructor will be called and the fields will be initialized.

If you don't do anything, the default constructor of the parent will be called. However, you can use the super keyword to invoke specific constructor on the parent class.

Uri
  • 84,589
  • 46
  • 214
  • 312
  • 7
    Normally, if you take the theory of constructor chaining into account, then the parent constructor is called first before the child constructor executes – Stranger Jun 23 '13 at 15:09
  • `Now, if you have two different subclasses of your abstract class, when you instantiate them their constructors will be called, and then the parent constructor will be called and the fields will be initialized.` You mean first child-classes' constructor will called? :O – Asif Mushtaq Mar 13 '16 at 13:49
  • Every time it flows like this - `grand parent constructor` -> `parent constructor` -> `child constructor` – Jignesh M. Khatri Sep 14 '17 at 18:13
  • @Stranger "the parent constructor is called first before the child constructor executes" I want to know more about this and to take the theory of java constructor chaining. Do you have any useful link or ebook or something? – Nyein Chan Jan 10 '18 at 05:00
13

Two reasons for this:

1) Abstract classes have constructors and those constructors are always invoked when a concrete subclass is instantiated. We know that when we are going to instantiate a class, we always use constructor of that class. Now every constructor invokes the constructor of its super class with an implicit call to super().

2) We know constructor are also used to initialize fields of a class. We also know that abstract classes may contain fields and sometimes they need to be initialized somehow by using constructor.

Panagiotis Kanavos
  • 90,087
  • 9
  • 138
  • 171
Debmalya
  • 131
  • 1
  • 3
11

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

Upul Bandara
  • 5,769
  • 4
  • 32
  • 59
7

Because another class could extend it, and the child class needs to invoke a superclass constructor.

missingfaktor
  • 86,952
  • 56
  • 271
  • 360
Richard JP Le Guen
  • 26,771
  • 7
  • 80
  • 113
5

I guess root of this question is that people believe that a call to a constructor creates the object. That is not the case. Java nowhere claims that a constructor call creates an object. It just does what we want constructor to do, like initialising some fields..that's all. So an abstract class's constructor being called doesn't mean that its object is created.

Mandroid
  • 3,359
  • 3
  • 28
  • 59
4

Because abstract classes have state (fields) and somethimes they need to be initialized somehow.

1

Implementation wise you will often see inside super() statement in subclasses constructors, something like:


public class A extends AbstractB{

  public A(...){
     super(String constructorArgForB, ...);
     ...
  }
}


manuel aldana
  • 13,184
  • 8
  • 40
  • 49