Inheritance in Python - Object-Oriented Programming PDF

Summary

This presentation introduces the concept of inheritance in Python, demonstrating single, multiple, and multilevel inheritance. It also covers method overriding and the super() function, providing code examples to illustrate how object-oriented programming principles facilitate code reuse and organization. The document is intended to provide an introduction to Python class inheritance.

Full Transcript

Inheritance There are 5 different types of inheritance in Python. They Inheritance...

Inheritance There are 5 different types of inheritance in Python. They Inheritance are: Single Inheritance: a child class inherits from only one Inheritance allows us to define a class that inherits all the methods and properties from parent class. Multiple Inheritance: a another class. child class inherits from multiple parent classes. Parent class is the class being inherited from, also called base/super class. Multilevel Inheritance: a child class inherits from its Child class is the class that inherits from another class, also called derived class. parent class, which is inheriting from its parent # define a superclass class. class super_class: # attributes and method definition Hierarchical Inheritance: more than one child class are # inheritance created from a single parent class sub_class(super_class): class. # attributes and method of super_class # attributes and method of sub_class Hybrid Inheritance: combines more than one form of inheritance. Inheritance class Car: # attribute and method of the parent class name = "" model= "" def CName(self): print("Name of car is ", self.name) # inherit from Car class class Model(Car): # new method in subclass def CModel(self): # access name attribute of superclass using self print("Car model is ", self.model) # create an object of the subclass carname = Model() # access superclass attribute and method carname.name = "Audi" carname.model = 2025 carname.CName() carname.CModel() Method Overriding in Python Inheritance Child class defines a method that has the class Car: # attribute and method of the parent class same name and parameters as a method in name = "" its parent class. model= "" def CName(self): print("Name of car is ", self.name) The method in the subclass overrides the method in the superclass. This concept is # inherit from Car class class Model(Car): known as method overriding in Python. # new method in subclass def CName(self): # access name attribute of superclass using self print("Car model is ", self.model) # create an object of the subclass carname = Model() # access superclass attribute and method carname.name = "Audi" carname.model = 2025 carname.CName() super() Function in Inheritance If we need to access the superclass class Car: method from the subclass, we use the # attribute and method of the parent class super() function. name = "" model= "" def CName(self): The same method (function) in the print("Name of car is ", self.name) subclass overrides the method in the superclass. # inherit from Car class class Model(Car): # new method in subclass def CName(self): super().CName() # access name attribute of superclass using self print("Car model is ", self.model) # create an object of the subclass carname = Model() # access superclass attribute and method carname.name = "Audi" carname.model = 2025 carname.CName() Multiple Inheritance class VehicleName: name = "" Multiple Inheritance: a child class model= "" inherits from multiple parent classes. def VName(self): print("Name of Vehicle is ", self.name) class VehicleModel: def VModel(self): print("Vehicle model is ", self.model) class VehicleType(VehicleName,VehicleModel): pass VehicleName = VehicleType() # access superclass attribute and method VehicleName.name = "Audi" VehicleName.model = 2025 VehicleName.VName() VehicleName.VModel() Multilevel Inheritance class SuperClass: def super_method(self): Multilevel Inheritance: a child class inherits from its parent class, which is print("Super Class method called") inheriting from its parent class. class DerivedClass1(SuperClass): def derived1_method(self): print("Derived class 1 method called") class DerivedClass2(DerivedClass1): def derived2_method(self): print("Derived class 2 method called") d2 = DerivedClass2() d2.super_method() d2.derived1_method() d2.derived2_method() Association: Association is a relationship between two or more objects that enables one object to interact with another. It defines how objects of different classes are connected and how they communicate with each other. Association represents a "uses-a" or "works-with" relationship, without implying ownership between the objects. Types of Association There are two main types of association: 1. Unidirectional Association In a unidirectional association, one object knows about the other and can access its behavior or data, but the reverse is not true. 2. Bidirectional Association In a bidirectional association, both objects are aware of each other and can interact. Unidirectional Association: Example class Driver: def __init__(self, name): self.name = name class Car: def __init__(self, driver): self.driver = driver def show_driver(self): print(f"The driver of this car is {self.driver.name}.") driver = Driver("Alice") car = Car(driver) car.show_driver() # Output: The driver of this car is Alice Bidirectional Association: Example class Teacher: def __init__(self, name): self.name = name self.students = [] def add_student(self, student): self.students.append(student) student.teacher = self class Student: def __init__(self, name): self.name = name self.teacher = None teacher = Teacher("Mr. Smith") student = Student("John") teacher.add_student(student) print(f"{student.name}'s teacher is {student.teacher.name}.") print(f"{teacher.name} teaches {teacher.students.name}.") Polymorphism It refers to the use of a single type entity (method, operator or object) to represent different types in different scenarios. For integer data types, + operator is used to perform arithmetic addition operation. num1 = 1 num2 = 2 print(num1+num2) For string data types, + operator is used to perform concatenation str1 = "Python" str2 = "Programming" print(str1+" "+str2) Function print(len("Programming")) print(len(["Python", "Java", "C"])) print(len({"Name": "John", "Address": “India"})) Polymorphism It refers to the use of a single type entity (method, operator or object) to represent different types in different scenarios. For integer data types, + operator is used to perform arithmetic addition operation. num1 = 1 num2 = 2 print(num1+num2) For string data types, + operator is used to perform concatenation str1 = "Python" str2 = "Programming" print(str1+" "+str2) Function print(len("Programming")) print(len(["Python", "Java", "C"])) print(len({"Name": "John", "Address": “India"}))

Use Quizgecko on...
Browser
Browser