Inheritance
CMSC 203 – Object-Oriented Programming
Module 4
Inheritance
• A fundamental concept in object-oriented
programming (OOP) that allows a new class (called
a derived or child class) to acquire the properties
and behaviors (fields and methods) of an existing
class (called a base or parent class).
• This mechanism is central to reusability, modularity,
and the organization of code.
Key Concepts
Base (Parent) Class
• The class that is being inherited from.
• It provides properties (attributes) and methods
(functions) that are common to the derived classes.
Derived (Child) Class
• The class that inherits from the base class.
• It can use the methods and properties of the base class
and also add its own.
Inheritance Syntax
• In most programming languages, inheritance is
declared by specifying the base class in parentheses
after the derived class name.
Methods Overloading
• A derived class can override (or replace) a method
from the base class to provide a specific
implementation.
Super () Function
• In some languages like Python, super () is used within a
derived class to call a method from the base class,
allowing the derived class to extend or modify the
behavior of the base class.
Single vs Multiple Inheritance
Single Inheritance
• A derived class inherits from only one base class.
Multiple Inheritance
• A derived class inherits from more than one base class.
• This can introduce complexity and is handled
differently in various programming languages
Single vs Multiple Inheritance
• Protected members are accessible within the class and
by derived classes, while private members are only
accessible within the class itself.
Polymorphism
• Inheritance supports polymorphism, where a derived
class can be treated as an instance of its base class.
• This allows for flexibility and interchangeability in the
code.
Benefits of Inheritance
Code Reusability
• Common functionality can be written once in the base
class and reused by derived classes, reducing
redundancy.
Code Organization
• Inheritance promotes a hierarchical class structure that
logically organizes code and simplifies maintenance.
Extensibility
• New classes can be created by extending existing ones,
making it easier to introduce new functionality without
altering existing code.
Method Overriding
• Occurs when a subclass (child class) provides a
specific implementation for a method that is already
defined in its superclass (parent class).
• The overridden method in the subclass should have the
same name, return type, and parameters as the method
in the superclass.
Purpose of Method Overriding
• To allow a subclass to provide a specific
implementation of a method that is already defined in
its superclass.
• Enables polymorphism, where a subclass can be treated
as an instance of its superclass, but it can behave
differently depending on the method implementation in
the subclass.
Method Overloading
• Occurs when multiple methods with the same name are
defined in the same class, but with different parameter
lists (different types, numbers, or both).
• The return type of the methods can be the same or
different.
Simulating Method Overloading
• Though Python doesn't support method overloading
directly, you can achieve similar functionality by using
default arguments, variable-length arguments ( *args and
**kwargs), or by explicitly checking the types and
numbers of arguments within a single method.
Simulating Method Overloading
Using Default Arguments
• You can define a method with default arguments to
handle different cases:
Using Variable - Length Arguments (*args)
• You can use *args to accept a variable number of
arguments:
Key Differences
Overriding
• Involves two classes (a superclass and a subclass).
• The method signature (name, parameters, and return
type) must be the same.
• Supports runtime polymorphism.
Overloading
• Involves a single class.
• The method name is the same, but the parameters are
different.
• Supports compile-time polymorphism.
THANKS!
CREDITS: This presentation template was created by Slidesgo, and
includes icons by Flaticon, and infographics & images by Freepik