Home >Backend Development >C++ >Function rewriting and abstract methods: Understand the necessity of subclasses to implement abstract methods of parent classes
Function overriding allows subclasses to redefine parent class methods, while abstract methods force subclasses to implement unimplemented methods of the parent class. It is crucial for subclasses to implement abstract methods of the parent class because it: improves code flexibility and extensibility; reduces code redundancy and promotes reuse; and enhances testability, allowing easy verification that the subclass correctly implements the parent class interface.
Introduction
In object-oriented programming, function overriding and abstract methods are two key concepts that allow us to create flexible and extensible class hierarchies. In this article, we will explore these two concepts and understand their differences through practical examples.
Function rewriting
Function rewriting refers to redefining methods that already exist in the parent class in the subclass. This allows subclasses to customize the parent class's methods while still retaining its core functionality. The syntax is as follows:
class Parent: def foo(self): print("Parent foo") class Child(Parent): def foo(self): print("Child foo")
In the above example, the Child
class overrides the foo
method and prints a different message.
Abstract method
An abstract method is a method that does not provide an implementation. It forces subclasses to implement the method before instantiation. The syntax is as follows:
from abc import ABC, abstractmethod class Parent(ABC): @abstractmethod def foo(self): pass class Child(Parent): def foo(self): print("Child foo")
In this example, the Parent
class contains an abstract method foo
. To instantiate the Child
class, we must provide an implementation of the foo
method. If we don't do this, a NotImplementedError
exception will occur.
The necessity for subclasses to implement abstract methods of parent classes
It is crucial for subclasses to implement abstract methods of parent classes because it allows parent classes to define abstract interfaces, and children Classes provide concrete implementations. This helps in the following areas:
Practical case
Suppose we are developing a graphics library. We can create an abstract Shape
class that defines the basic geometric properties and drawing methods of the shape:
from abc import ABC, abstractmethod class Shape(ABC): def __init__(self, color, x, y): self.color = color self.x = x self.y = y @abstractmethod def draw(self): pass
We can then create subclasses Square
andCircle
, respectively implement drawing squares and circles:
class Square(Shape): def __init__(self, color, x, y, side_length): super().__init__(color, x, y) self.side_length = side_length def draw(self): print(f"Drawing a square with color {self.color}, x {self.x}, y {self.y}, and side length {self.side_length}") class Circle(Shape): def __init__(self, color, x, y, radius): super().__init__(color, x, y) self.radius = radius def draw(self): print(f"Drawing a circle with color {self.color}, x {self.x}, y {self.y}, and radius {self.radius}")
By using the abstract method draw
, we can ensure that all shapes can be drawn, while allowing subclasses to provide their own specific implementation.
The above is the detailed content of Function rewriting and abstract methods: Understand the necessity of subclasses to implement abstract methods of parent classes. For more information, please follow other related articles on the PHP Chinese website!