You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers abstract classes and interfaces — two mechanisms for achieving abstraction in Object-Oriented Programming. These features enforce structure in class hierarchies and are fundamental to designing large-scale, maintainable software systems.
Abstraction is the process of hiding complex implementation details and exposing only the essential features of an object. It allows programmers to work with high-level concepts without needing to understand every internal detail.
Consider a car: you interact with the steering wheel, pedals, and gear stick (the interface), but you do not need to understand the internal combustion engine (the implementation).
In OOP, abstraction is achieved through:
An abstract class is a class that:
Abstract classes serve as templates that force subclasses to implement specific behaviour.
ABSTRACT CLASS Shape
PRIVATE colour: STRING
PUBLIC PROCEDURE new(c: STRING)
colour = c
END PROCEDURE
PUBLIC FUNCTION getColour() RETURNS STRING
RETURN colour
END FUNCTION
ABSTRACT FUNCTION area() RETURNS REAL
ABSTRACT FUNCTION perimeter() RETURNS REAL
PUBLIC FUNCTION describe() RETURNS STRING
RETURN "A " + colour + " shape with area " + STR(area())
END FUNCTION
END CLASS
CLASS Circle INHERITS Shape
PRIVATE radius: REAL
PUBLIC PROCEDURE new(c: STRING, r: REAL)
SUPER.new(c)
radius = r
END PROCEDURE
PUBLIC FUNCTION area() RETURNS REAL
RETURN 3.14159 * radius * radius
END FUNCTION
PUBLIC FUNCTION perimeter() RETURNS REAL
RETURN 2 * 3.14159 * radius
END FUNCTION
END CLASS
from abc import ABC, abstractmethod
class Shape(ABC):
def __init__(self, colour: str):
self.__colour = colour
def get_colour(self) -> str:
return self.__colour
@abstractmethod
def area(self) -> float:
pass
@abstractmethod
def perimeter(self) -> float:
pass
def describe(self) -> str:
return f"A {self.__colour} shape with area {self.area():.2f}"
class Circle(Shape):
def __init__(self, colour: str, radius: float):
super().__init__(colour)
self.__radius = radius
def area(self) -> float:
return 3.14159 * self.__radius ** 2
def perimeter(self) -> float:
return 2 * 3.14159 * self.__radius
# shape = Shape("red") # ERROR: cannot instantiate abstract class
circle = Circle("blue", 5)
print(circle.describe()) # A blue shape with area 78.54
| Use Case | Explanation |
|---|---|
| Shared implementation | When subclasses share some common code but differ in specific methods. |
| Enforcing structure | When you want to guarantee that subclasses implement certain methods. |
| Partial abstraction | When some methods have default implementations and others must be overridden. |
Exam Tip: If asked "What is the difference between an abstract class and a normal class?", the key points are: (1) abstract classes cannot be instantiated, (2) they can contain abstract methods without implementations, and (3) subclasses must implement all abstract methods.
An interface defines a contract — a set of method signatures that any implementing class must provide. Unlike abstract classes, interfaces traditionally contain no implementation and no attributes (though modern languages have relaxed this).
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.