What is the use of constructors in abstract classes?

Since we cannot create an instance of an abstract class, then what is the need for constructors in the abstract class?

+3
source share
4 answers

Abstract classes are designed to be extended, each constructor from a child must call the constructor from the base class , so you need constructors in the abstract class.

An abstract class is a skeleton and therefore it makes no sense to instantiate it directly, since it is still incomplete (children will provide the rest).

+4
source

Example:

public abstract class BaseClass
{
    private String member;

    public BaseClass(String member)
    {
        this.member = member;
    }

    ... abstract methods...
}

public class ImplementingClass extends BaseClass
{
    public ImplementingClass(String member)
    {
        /* Implementing class must call a constructor from the abstract class */
        super(member);
    }

    ... method implementations...
}
+2
source

, .

+2

( rater ). , , , , ,

super(foo);, ,

+1

Source: https://habr.com/ru/post/1752790/


All Articles