CMSC 203 Module 5
CMSC 203 Module 5
CMSC 203 Module 5
Abstract Class
• A class that cannot be instantiated directly and often
contains one or more abstract methods.
• Its purpose is to provide a common structure and
partially implemented functionality for its subclasses.
Key Characteristics of Abstract Class
• Cannot be instantiated.
• Can have both fully implemented methods and abstract
methods.
• Serves as a blueprint for other classes.
Use Case
• Abstract classes are useful when you want to provide a
shared interface and some functionality to multiple
related subclasses but also want to enforce certain
behaviors that subclasses must implement.
Abstract method
• A method that is declared in an abstract class but does
not have any implementation.
• It must be implemented by any subclass that inherits
from the abstract class.
Key Characteristics of Abstract Method
• Defined using the @abstractmethod decorator in
Python (other languages like Java use the abstract
keyword).
• Provides no functionality (only a method signature).
• Subclasses are required to provide an implementation
for the abstract method.
Use Case
• Abstract methods are used when you want to force
subclasses to implement certain behavior, ensuring
consistency across different subclasses.
How Abstract Classes and Methods
Work Together
• Abstract classes define common behaviors through
concrete methods (with implementation) and abstract
methods (without implementation).
• Subclasses inherit these behaviors but must implement
any abstract methods.
• This approach provides flexibility to define a
generalized framework (in the abstract class) while
allowing subclasses to provide specific functionality.
Benefits of Abstract Classes and
Methods
Encapsulation of Common Logic:
• Abstract classes allow you to define common behavior
in one place, reducing code duplication in subclasses.
Enforces a Contract:
• Abstract methods ensure that certain methods are
implemented in every subclass, providing consistency
across different classes.
Extensibility:
• Abstract classes make it easy to add new subclasses
without modifying existing code, following the
open/closed principle in software design.
THANKS!
CREDITS: This presentation template was created by Slidesgo, and
includes icons by Flaticon, and infographics & images by Freepik