Sobes.tech
Junior — Middle

Can you list the different types of inheritance in object-oriented programming?

sobes.tech AI

Answer from AI

In object-oriented programming, there are different types of inheritance that determine how classes can inherit properties and methods from each other:

  • Single inheritance — a class inherits from one base class.
  • Multiple inheritance — a class inherits from several base classes (for example, in C++).
  • Multilevel inheritance — a class inherits from a class that itself inherits from another class.
  • Hierarchical inheritance — several classes inherit from one base class.
  • Hybrid inheritance — a combination of several types of inheritance.

Example of single inheritance in C++:

class Animal {
public:
    void eat() {}
};

class Dog : public Animal {
public:
    void bark() {}
};

Here, the Dog class inherits the eat() method from Animal.