CMSC 203 Module 5

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Polymorphism

CMSC 203 – Object-Oriented Programming


Module 56
Polymorphism
• An essential concept in object-oriented programming
(OOP) that allows objects of different types to be
treated as objects of a common supertype.
• It provides flexibility and enables the same operation
or method to behave differently depending on the
object calling it.
Concepts of Polymorphism
Method Overloading (Compile-Time Polymorphism)
• Multiple methods have the same name but differ in the
number or type of parameters within the same class.
• The appropriate method is chosen based on the method
signature at compile time.
Method Overriding
• Occurs when a subclass provides a specific
implementation of a method that is already defined in
its superclass.
• The decision of which method to invoke is made at
runtime based on the actual type of the object.
Operator Overloading
• Defining special methods to give operators like +, -,
or * different meanings depending on the context
(objects of custom classes).
Polymorphism Function
• A polymorphic function works on different data types
or objects without requiring modification.
• These functions can take different types of objects and
perform the same operation on them based on shared
interfaces.
Inheritance and Polymorphism
• Polymorphism is often seen in the context of
inheritance, where a subclass can inherit and override
methods from a parent class, allowing it to present
different behavior even when referred to by a
superclass reference.
Interfaces and Polymorphism
• Polymorphism is often implemented using interfaces or
abstract base classes in many OOP languages.
• An interface defines methods that must be
implemented by a subclass, ensuring polymorphic
behavior.
Types of Polymorphism
Compile-Time Polymorphism (Static Polymorphism)
• Achieved through method overloading or operator
overloading.
• Multiple methods have the same name but differ in the
type or number of parameters.
• The method to execute is determined at compile time
based on the method signature.
Runtime Polymorphism (Dynamic Polymorphism
• Achieved through method overriding, where a
subclass provides a specific implementation of a
method that is already defined in its superclass.
• The method to execute is determined at runtime
based on the object's type
Static vs Dynamic Binding
Static Binding (Early Binding)
• Occurs when the method or function to be executed is
determined at compile time.
• In static binding, the type of the object or reference is
known at compile time, and the appropriate method is
called based on the type information available at that
point.
Methods Involved
• Static methods, overloaded methods, and final methods
typically use static binding because they are resolved
based on the reference type and not the actual object
type.
Performance
• Since the method to be invoked is already known at
compile time, static binding tends to be faster.
Flexibility
• Static binding offers less flexibility since the method
cannot change dynamically at runtime.
Common Use
• Static binding is generally used for method
overloading, early resolution of methods, and for static
or private methods.
Dynamic Binding (Late Binding)
• Occurs when the method to be invoked is determined
at runtime.
• The type of the actual object, not the reference, is used
to decide which method implementation should be
executed.
• Even though the reference might be of a superclass
type, the actual object's method (from a subclass) will
be executed.
Methods Involved
• Dynamic binding is used for method overriding
(polymorphism), where the subclass method overrides
the superclass method.
Performance
• Dynamic binding is slightly slower than static binding
because the method resolution happens at runtime.
Flexibility
• Dynamic binding provides greater flexibility, allowing
the program to decide at runtime which method to
invoke based on the actual object.
Common Use
• Dynamic binding is used for method overriding,
ensuring that the correct method is called for objects of
subclass types, even when accessed through a
superclass reference.
Comparison of Static vs Dynamic
Binding
Abstract Classes and Methods

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

You might also like