Junior — Middle
From which classes or structures can a base class be obtained in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, a base class (superclass) can be obtained from:
- Another class — a derived class extends or overrides the behavior of the base class.
- An abstract class — the base class can be abstract, meaning it contains abstract methods that must be implemented in subclasses.
- An interface (in languages supporting multiple inheritance of interfaces, e.g., Python with ABC) — a class can implement an interface that defines a set of methods without implementation.
In Python, a base class is any class or abstract base class (ABC) from which another class inherits. For example:
from abc import ABC, abstractmethod
class Base(ABC):
@abstractmethod
def do_something(self):
pass
class Derived(Base):
def do_something(self):
print("Method implementation")
Thus, a base class can be a regular class, an abstract class, or an interface (via ABC), from which other classes inherit.