Junior — Middle
What is the difference between declaring an abstract class and a regular class in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, an abstract class is a class that is not intended to be instantiated directly, but serves as a base class for other classes. It can contain abstract methods — methods without implementation that must be implemented in subclasses.
In Python, an abstract class is declared using the abc module and the @abstractmethod decorator:
from abc import ABC, abstractmethod
class AbstractClass(ABC):
@abstractmethod
def do_something(self):
pass
class ConcreteClass(AbstractClass):
def do_something(self):
print("Method implementation")
# AbstractClass() will raise an error, but ConcreteClass() can be instantiated
A regular class does not contain abstract methods and can be instantiated directly. An abstract class is used to define an interface and common logic that must be implemented by subclasses.