🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

CMPE-103-Module-5-Inheritance-and-Polymorphism.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

CMPE 103 Module 5 INHERITANCE & POLYMORPHISM At the end of the module, you should be able to: define Inheritance and Polymorphism discuss the syntax and program structure of Inheritance demonstrate application programs on inheritance discuss the types of inheritance...

CMPE 103 Module 5 INHERITANCE & POLYMORPHISM At the end of the module, you should be able to: define Inheritance and Polymorphism discuss the syntax and program structure of Inheritance demonstrate application programs on inheritance discuss the types of inheritance demonstrate application program on the different type of inheritance Intro to polymorphism concept discuss the concept of overloading and Overriding demonstrate application programs on overloading and overriding Inheritance The process of inheriting the properties of the parent class into a child class is called inheritance. The existing class is called a base class or parent class and the new class is called a subclass or child class or derived class. The main purpose of inheritance is the reusability of code because we can use the existing class to create a new class instead of creating it from scratch. In inheritance, the child class acquires all the data members, properties, and functions from the parent class. Also, a child class can also provide its specific implementation to the methods of the parent class For example, In the real world, Car is a sub-class of a Vehicle class. We can create a Car by inheriting the properties of a Vehicle such as Wheels, Colors, Fuel tank, engine, and add extra properties in Car as required. Inheritance – “IS A” or “IS A KIND OF” Relationship Example – “IS A” Relationship How to implement Inheritance in Python Create a Parent Class class Person: def __init__(self, fname, lname): Create a Child Class self.firstname = fname self.lastname = lname class Student(Person): pass def printname(self): print(self.firstname, self.lastname) ‘’’Use the Student class to create an object, and then execute the printname method’’’ ‘’’Use the Person class to create an x = Student(“Melchora", “Aquino") object, and then execute the printname x.printname() method’’’ x = Person(“Jose", “Rizal") x.printname() Types Of Inheritance In Python, based upon the number of child and parent classes involved, there are five types of inheritance. The type of inheritance are listed below: Single inheritance Multiple Inheritance Multilevel inheritance Hierarchical Inheritance Hybrid Inheritance Single Inheritance In single inheritance, a child class inherits from a single-parent class. Here is one child class and one parent class. Output: Multiple Inheritance In multiple inheritance, one child class can inherit from multiple parent classes. So here is one child class and multiple parent classes. Output: Multilevel inheritance In multilevel inheritance, a class inherits from a child class or derived class. Suppose three classes A, B, C. A is the superclass, B is the child class of A, C is the child class of B. In other words, we can say a chain of classes is called multilevel inheritance. Output: Hierarchical Inheritance In Hierarchical inheritance, more than one child class is derived from a single parent class. In other words, we can say one parent class and multiple child classes. Output: Hybrid Inheritance When inheritance is consists of multiple types or a combination of different inheritance is called hybrid inheritance. Output: Python super() function When a class inherits all properties and behavior from the parent class is called inheritance. In such a case, the inherited class is a sub class and the latter class is the parent class. In child class, we can refer to parent class by using the super()function. Benefits of using the super()function: ▪ We are not required to remember or specify the parent class name to access its methods. ▪ We can use the super()function in both single and multiple inheritances. The super() function support code reusability as there is no need to write the entire function Output: Method Overriding In inheritance, all members available in the parent class are by default available in the child class. If the child class does not satisfy with parent class implementation, then the child class is allowed to redefine that method by extending additional functions in the child class. This concept is called method overriding. When a child class method has the same name, same parameters, and same return type as a method in its superclass, then the method in the child is said to override the method in the parent class. Output: In the above example, we create two classes named Vehicle (Parent Class) and Car (Child Class) The class Car extends from the class Vehicle so, all properties of the parent class are available in the child class. In addition to that, the child class redefined the method max_speed(). What is Polymorphism in Python? Polymorphism in Python is the ability of an object to take many forms. In simple words, polymorphism allows us to perform the same action in many different ways. For example, Jessa acts as an employee when she is at the office. However, when she is at home, she acts like a wife. Also, she represents herself differently in different places. Therefore, the same person takes different forms as per the situation. Polymorphism in Built-in function len() The built-in function len() calculates the length of an object depending upon its type. If an object is a string, it returns the count of characters, and If an object is a list, it returns the count of items in a list. Output: Polymorphism With Inheritance Polymorphism is mainly used with inheritance. In inheritance, child class inherits the attributes and methods of a parent class. The existing class is called a base class or parent class, and the new class is called a subclass or child class or derived class. Using method overriding polymorphism allows us to defines methods in the child class that have the same name as the methods in the parent class. This process of re-implementing the inherited method in the child class is known as Method Overriding. Advantage of method overriding It is effective when we want to extend the functionality by altering the inherited method. Or the method inherited from the parent class doesn’t fulfill the need of a child class, so we need to re-implement the same method in the child class in a different way. Method overriding is useful when a parent class has multiple child classes, and one of that child class wants to redefine the method. The other child classes can use the parent class method. Due to this, we don’t need to modification the parent class code In polymorphism, Python first checks the object’s class type and executes the appropriate method when we call the method. Example of Polymorphism in Inheritance Output:

Use Quizgecko on...
Browser
Browser