Sobes.tech
Junior — Middle

Explain the concept of abstract classes in object-oriented programming.

sobes.tech AI

Answer from AI

An abstract class is a class that is not intended to be instantiated directly, but serves as a base for other classes. It can contain both implemented methods and abstract methods (without implementation), which must be overridden in subclasses.

This allows defining a common interface and behavior for a group of related classes, providing polymorphism and code reuse. For example, an abstract class Animal might have an abstract method makeSound(), which specific animals implement:

abstract class Animal {
    abstract void makeSound();

    void sleep() {
        System.out.println("Sleeping...");
    }
}

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

Here, you cannot create new Animal(), but you can create new Dog(), which implements all abstract methods.

Explain the concept of abstract classes in… - sobes.tech