Sobes.tech
Junior — Middle

Can you explain what an abstract class is in object-oriented programming?

sobes.tech AI

Answer from AI

An abstract class in object-oriented programming 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) that must be overridden in derived classes.

In C++, an abstract class is defined by having at least one pure virtual function:

class AbstractClass {
public:
    virtual void doSomething() = 0; // pure virtual function
    void commonMethod() {
        // common implementation
    }
};

class Derived : public AbstractClass {
public:
    void doSomething() override {
        // specific implementation
    }
};

Such a class defines an interface and common functionality, enabling polymorphism and the creation of class hierarchies with mandatory overriding of key methods.