Midterm Lesson 4 - Python Inheritance and Polymorphism

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

15 Terms

1
New cards

Inheritance

  • One of the core concept in object oriented programming.

  • It is a mechanism that allows you to create hierarchy of classes that share a set of properties and method by deriving a class from another class.

  • Inheritance is to capability of one class to derive or inherit the properties from another class.

  • Allows you to inherit the properties of a class example based glass to another derived class.

2
New cards

Super Class (Parent/Base Class)

  • The class whose feature are inherited by another classes.

3
New cards

Sub Class (Child Class)

  • The class that inherits the feature of other classes. subclasses can also have their own features in addition to inherit one.

4
New cards

Reusability

  • The concept where we can derive a new class from existing classes.

5
New cards

Benefits of Inheritance in Python

  • It represents real world relationships well.

  • It provides the reusability of a code. We don't have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.

  • It is transitive in nature which means that if class why it herits from another class x then all the subclasses of y would automatically inherit from class x.

  • Inheritance offer a simple, understandable modal structure

  • Less development and maintenance expenses result from an inheritance.

6
New cards

IS-A relationship

  • To further understand this mechanism of OOP, we use the IS-A relationship, where we create a subclass by including the name of the parent class as a parameter.

  • Considering the IS-A relationship, we can say that:

    Sedan IS-A Car

    Pickup IS-A Car

    Toyota IS-A Sedan and Pickup

    Hence: Toyota IS-A Car

<ul><li><p>To further understand this mechanism of OOP, we use the IS-A relationship, where we create a subclass by including the name of the parent class as a parameter.</p><p></p></li><li><p>Considering the IS-A relationship, we can say that: </p><p>Sedan IS-A Car </p><p>Pickup IS-A Car </p><p>Toyota IS-A Sedan and Pickup </p><p>Hence: Toyota IS-A Car</p></li></ul><img src="https://knowt-user-attachments.s3.amazonaws.com/50855a3d-53cd-497e-8e9f-52b3c4d75005.png" data-width="100%" data-align="center"><p></p>
7
New cards

Subclassing

  • A child class need to identify which class is its parent class.

  • This can be done by mentioning the parent class name in the definition of the child class.

  • In order to properly use the attributes of a parent class you must either:

    • Invoke the constructor of the parent class

    • Do not create a constructor for the subclass and use getter and setters instead

  • Note: You can also use the super() function to access the parent’s class methods and functions.

<ul><li><p>A child class need to identify which class is its parent class.</p></li><li><p>This can be done by <strong>mentioning the parent class</strong> name in the definition of the child class.</p></li><li><p>In order to properly use the attributes of a parent class you must either:</p><ul><li><p><strong>Invoke the constructor of the parent class</strong> </p></li><li><p><strong>Do not create a constructor for the subclass and use getter and setters instead</strong></p></li></ul></li><li><p>Note: You can also use the super() function to access the parent’s class methods and functions. </p></li></ul><p></p>
8
New cards

Single Inheritance

Types of Inheritance:

  • Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling code reusability and the addition of new features to existing code.

<p>Types of Inheritance:</p><p></p><ul><li><p>Single inheritance <strong>enables a derived class to inherit properties from a single parent class</strong>, thus enabling code reusability and the addition of new features to existing code.</p></li></ul><p></p>
9
New cards

Multiple Inheritance

Types of Inheritance:

  • When a class is derived from more than one base class, this type of inheritance is called multiple inheritance.

  • In multiple inheritance, all the features of the base classes are inherited into the derived class.

<p>Types of Inheritance:</p><p></p><ul><li><p>When <strong>a class is derived from more than one base class</strong>, this type of inheritance is called <strong>multiple inheritance.</strong></p></li><li><p>In multiple inheritance, all the features of the base classes are inherited into the derived class.</p></li></ul><p></p>
10
New cards

Multilevel Inheritance

Types of 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 a grandfather.

<p>Types of Inheritance:</p><p></p><ul><li><p>In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class. </p></li><li><p>This is similar to a relationship <strong>representing a child and a grandfather.  </strong></p></li></ul><p></p>
11
New cards

Hierarchical Inheritance

Types of Inheritance:

  • When more than one derived class 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 or more child (derived) classes.

<p>Types of Inheritance:</p><p></p><ul><li><p>When <strong>more than one derived class are created from a single base,</strong> this type of inheritance is called <strong>hierarchical inheritance.</strong> </p></li><li><p>In this program, we have a parent (base) class and two or more child (derived) classes. </p></li></ul><p></p>
12
New cards

Hybrid Inheritance

Types of Inheritance:

  • Inheritance considering of multiplle types of inheritance is called hybrid inheritance.

13
New cards

Polymorphism

  • The word polymorphism means having many forms.

  • In programming, polymorphism means the same function name (but different signatures) being used for different types.

  • The key difference is the data types and number of arguments used in function.

<ul><li><p>The word polymorphism means <strong>having many forms. </strong></p><p> </p></li><li><p>In programming, polymorphism means the <strong>same function name (but different signatures) being used for different types.</strong></p><p> </p></li><li><p>The key difference is <strong>the data types</strong> and <strong>number of arguments used</strong> in function. </p></li></ul><p></p>
14
New cards

Polymorphism with class methods

  • Python can use two different classes in the same way.

  • We create a for loop that iterates through a tuple of objects then call the methods without being concerned about which class type each object is.

  • We assume that these methods actually exist in each class.

<ul><li><p>Python can use <strong>two different classes in the same way.</strong></p><p></p></li><li><p>We create a for loop that iterates through a tuple of objects then call the methods without being concerned about which class type each object is.</p><p></p></li><li><p>We assume that <strong>these methods actually exist in each class. </strong></p></li></ul><p></p>
15
New cards

Polymorphism with Inheritance

  • In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class.

  • However, it is possible to modify a method in a child class that it has inherited from the parent class.

  • This is particularly useful in cases where the method inherited from the parent class doesn’t quite fit the child class.

  • In such cases, we re-implement the method in the child class. This process of re-implementing a method in the child class is known as Method Overriding.

<ul><li><p>In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class <strong>inherits the methods from the parent class.</strong></p><p></p></li><li><p>However, it is possible to <strong>modify a method in a child class that it has inherited from the parent class.</strong></p><p>  </p></li><li><p>This is particularly useful in cases where the method inherited from the parent class <strong>doesn’t quite fit the child class.</strong></p><p></p></li><li><p>In such cases, we <strong>re-implement the method in the child class.</strong> This process of re-implementing a method in the child class is known as <strong>Method Overriding. </strong></p></li></ul><p></p>