Sobes.tech
Junior — Middle

What restrictions do abstract classes have in object-oriented programming?

sobes.tech AI

Answer from AI

Abstract classes in object-oriented programming have the following limitations:

  • Cannot instantiate an abstract class directly. An abstract class is intended for inheritance, not for creating objects directly.

  • Can contain both abstract methods (without implementation) and methods with implementation. But if a class contains at least one abstract method, it must be declared abstract.

  • Restriction on multiple inheritance. In languages like Java, a class can inherit from only one abstract class, which limits flexibility compared to interfaces.

  • Cannot be used to implement multiple inheritance of behavior. For this, interfaces are better suited.

  • An abstract class can have state (fields), which sometimes complicates design and code reuse.

Example of a limitation:

abstract class Animal {
    abstract void makeSound();
}

// Cannot create an Animal object directly:
// Animal a = new Animal(); // Compilation error

class Dog extends Animal {
    @Override
    void makeSound() {
        System.out.println("Woof");
    }
}

Thus, abstract classes serve as templates with partial implementation but have limitations related to inheritance and instantiation.

What restrictions do abstract classes have in… - sobes.tech