Szere Dyeri Szere Dyeri - 2 years ago 72
Java Question

Can an abstract class have a constructor?

Can an abstract class have a constructor?

If so, how it can be used and for what purposes?

Answer Source

Yes, an abstract class can have a constructor. Consider this:

abstract class Product { 
    int multiplyBy;
    public Product( int multiplyBy ) {
        this.multiplyBy = multiplyBy;

    public int mutiply(int val) {
       return multiplyBy * val;

class TimesTwo extends Product {
    public TimesTwo() {

class TimesWhat extends Product {
    public TimesWhat(int what) {

The superclass Product is abstract and has a constructor. The concrete class TimesTwo has a constructor that just hardcodes the value 2. The concrete class TimesWhat has a constructor that allows the caller to specify the value.

Abstract constructors will frequently be used to enforce class constraints or invariants such as the minimum fields required to setup the class.

NOTE: As there is no default (or no-arg) constructor in the parent abstract class the constructor used in subclasses must be specified.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download