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

前端 未结 8 2067
时光取名叫无心
时光取名叫无心 2020-12-03 18:50

I would like to know what purpose a constructor for an abstract class serves; as we do not instantiate abstract classes, why would we ever need such a constructor?

8条回答
  •  春和景丽
    2020-12-03 19:35

    If you have uninitialised final fields in an abstract class, you'll need to initialise them in a constructor.

    E.g.

    abstract class A {
        final int x;
    }
    

    will not compile without a constructor that assigns to x.

提交回复
热议问题