0% found this document useful (0 votes)
20 views26 pages

CHAPTER22

class and object

Uploaded by

arulmr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views26 pages

CHAPTER22

class and object

Uploaded by

arulmr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Inheritance in Python

• Inheritance is a feature of object-oriented programming that allows a class to inherit


attributes and methods from another class. The class that inherits is called the child
class or subclass, and the class being inherited from is called the parent class or
superclass.
• Following are the benefits of inheritance:
Code reusability- we do not have to write the same code again and
again, we can just inherit the properties we need in a child class.
It represents a real world relationship between parent class and
child class.
It is transitive in nature. If a child class inherits properties from a
parent class, then all other sub-classes of the child class will also
inherit the properties of the parent class.
Syntax
class parent_class:
body of parent class

class child_class( parent_class):


body of child class

By using inheritance,we can create a class which uses all the


properties and behaviour of another class. The new class is
also known as derived class or child class, and the one whose
properties are acquired is known as base class or parent class.
Example
class Parent:
def first(self):
print('fisrt function')
class Child(Parent):
def second(self):
print('second function')
ob=child()
ob.first()
ob.second()
Types of Inheritance:

1.Single Inheritance: A child class inherits from a single parent class.


2.Multiple Inheritance: A child class inherits from more than one
parent class.
3.Multilevel Inheritance: A class is derived from another derived class.
4.Hierarchical Inheritance: Multiple child classes inherit from a single
parent class.
5.Hybrid Inheritance: A combination of two or more types of
inheritance
2. Multi Level Inheritance :
In multilevel inheritance, features of the base class and the
derived class are further inherited into the new derived
class. This is similar to a relationship representing a child
and grandfather.
4 . Hierarchical Inheritance :
When more than one derived classes are created from a single base this
type of inheritance is called hierarchical inheritance. In this program, we
have a parent (base) class and two child (derived) classes.
Polymorphism
Polymorphism in Python
• Polymorphism means "many forms" and it allows objects of different
classes to be treated as objects of a common superclass.
• Polymorphism is an important concept in Object-Oriented Programming
(OOP) that allows methods to be used interchangeably, enhancing
flexibility and integration.
• Types of Polymorphism
1.Compile-Time Polymorphism (also known as Static Polymorphism)
2.Run-Time Polymorphism (also known as Dynamic Polymorphism)
• In Python, polymorphism is primarily supported through dynamic
polymorphism.
Method Overriding
• Allows a child class to provide a specific implementation of a method
that is already defined in its parent class.
• Used to achieve run-time polymorphism.
Method Overloading
• Method overloading is not directly supported in Python, but it can be
achieved using default arguments or variable-length arguments.
Polymorphism with Functions
• The same function name can be used for different types of objects.
Function Polymorphism in Python

• There are some functions in Python which are compatible to run with
multiple data types.
• One such function is the len() function. It can run with many data types in
Python. Let's look at some example use cases of the function.
• Examples Polymorphic len() function
print(len("Devincept"))
print(len(["Python", "Java", "C"]))
print(len({"Name": "John", "Address": "Nepal"}))
• Output: 9 3 2 Here, we can see that many data types such as string, list,
tuple, set, and dictionary can work with the len() function. However, we can
see that it returns specific information about specific data types.
Encapsulation in Python
• Encapsulation is also an essential aspect of object-oriented
programming.
• It is used to restrict access to methods and variables.
• In encapsulation, code and data are wrapped together within a single
unit from being modified by accident.
• This puts restrictions on accessing variables and methods directly
and can prevent the accidental modification of data.
• To prevent accidental change, an object’s variable can only be
changed by an object’s method.
• Those types of variables are known as private variable.
• A class is an example of encapsulation as it encapsulates all the data
that is member functions, variables, etc.
Key Concepts
1.Data Hiding: Protecting the internal state of an object from
unintended modification by restricting access to certain components.
2.Access Modifiers: Keywords that set the visibility of class members
(attributes and methods).
Access Modifiers
•Python does not have explicit keywords for private, protected, or public access
modifiers. Instead, it uses naming conventions:
•Public: Accessible from outside the class. (No leading underscore)
•Protected: Intended to be accessed by subclasses. (Single leading underscore,
e.g., _attribute)
•Private: Intended to be hidden from outside the class. (Double leading
underscore, e.g., __attribute)
Protected Access Modifier

Attributes and methods defined as protected are intended to be


accessed within the class and its subclasses.
class Car:
def __init__(self, make, model):
self._make = make # Protected attribute
self._model = model # Protected attribute

def _display_info(self): # Protected method


print(f"Make: {_make}, Model: {_model}")

class ElectricCar(Car):
def __init__(self, make, model, battery):
super().__init__(make, model)
self.battery = battery
def display_info(self):
print(f"Make: {self._make}, Model: {self._model}, Battery: {self.battery}")

my_electric_car = ElectricCar("Tesla", "Model S", "100 kWh")


my_electric_car.display_info() # Output: Make: Tesla, Model: Model S, Battery: 100 kWh

You might also like