Sobes.tech
Junior — Middle

Can you declare a constructor inside an abstract class?

sobes.tech AI

Answer from AI

Yes, in Java, constructors can be declared inside an abstract class. An abstract class cannot be instantiated directly, but its constructors are called when creating objects of subclasses. This allows initializing common fields and performing shared logic when creating subclasses.

Example:

abstract class Animal {
    String name;

    public Animal(String name) {
        this.name = name;
    }
}

class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
}
Can you declare a constructor inside an abstract… - sobes.tech