Introduction To Method Overriding
Introduction To Method Overriding
Method Overriding
Method overriding is a fundamental concept in object-oriented
programming (OOP) where a subclass provides its own implementation
of a method that is already defined in its parent class. This allows for
dynamic dispatch and polymorphism, enabling flexible and extensible
code.
Defining Method Overriding
1 Overriding a Method
When a subclass provides its own implementation of a method that
already exists in the parent class.
2 Polymorphism
The ability of objects of different classes to respond to the same method
call in their own way.
3 Dynamic Dispatch
The process of selecting the appropriate method implementation at
runtime based on the actual type of the object.
Syntax for Method Overriding
Base Class Method
The method in the parent/base class
that will be overridden.
Access Modifier
The access modifier of the overriding
method must be the same or more
accessible than the base class
method.
Syntax for Method Overriding
using virtual
functions
Virtual function is a
provides method
overriding and pure
virtual function
provides Abstraction
which we will cover
in the next lecture.
Note: Override
keyword can only be
used if the method is
virtual.
Rules for Method Overriding
Return Type
The return type of the overriding method must be either the same or a
subtype of the base class method's return type.
Argument List
The argument list of the overriding method must have the same number
and type of parameters as the base class method.
Access Modifier
The access modifier of the overriding method must be the same or more
accessible than the base class method.
Exception Handling
The overriding method can only throw exceptions that are the same or
subclasses of the exceptions thrown by the base class method.
Advantages of Method Overriding
Flexibility
Allows subclasses to provide their own implementation of a method,
enabling polymorphism and dynamic dispatch.
Extensibility
Enables the addition of new functionality to a program without
modifying existing code.
Reusability
Allows code reuse by inheriting and overriding methods from parent
classes.
Maintainability
Simplifies code maintenance by localizing changes to the overriding
method in the subclass.
Differences between Method Overriding and
Method Overloading
Method Overriding Method Overloading Binding
Occurs in inheritance hierarchies, Allows a class to have multiple Overriding uses dynamic binding,
where a subclass provides its own methods with the same name but while overloading uses static
implementation of a method defined different parameter lists, providing binding.
in the parent class. method polymorphism.
Overriding vs Overloading: When
to Use Each
Inheritance
Use overriding when you have a subclass that needs to provide its own
implementation of a method from the parent class.
Same Class
Use overloading when you want to provide multiple methods with the
same name but different parameters within the same class.
Flexibility
Overriding offers more flexibility, while overloading provides more
method options within a class.
Conclusion and Key Takeaways
1 Overriding Enables Polymorphism
Method overriding allows subclasses to provide their own implementations, enabling dynamic
dispatch and polymorphism.
3 Offers Advantages
Overriding provides flexibility, extensibility, reusability, and maintainability to object-oriented
code.
4 Choose Wisely
Understand the differences between overriding and overloading to apply the appropriate
technique in your C++ code.