For Enquiry: 93450 45466

Types Of Inheritance In Python



Let’s discuss the following things in this blog.

What is inheritance
Types in inheritance
Single Level Inheritance
Multi-Level Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance
The __init__ method
super() charge method

You might have some resemblance or inherited behaviours or traits in you from your family’s lineage. Just like this, you can inherit properties of one class from another one.The newly inherited class is a child or derived class and the former as parent or base class.Inheritance emphasizes the DRY(Don’t Repeat Yourself) rule, with code reusability.

Here is an example of Inheritance

class Parent:

   def func1(self):

       print(“I am in parent class”)

class Child(Parent):

    def func2(self):

       print(“I am in derived class”)

obj = Child()

obj.func1()

obj.func2()

Single Level Inheritance

Check out this Complete Online Python Course by FITA. FITA provides a complete Python course where you will be building real-time projects like Bitly and Twitter bundled with Django, placement support and certification at an affordable price.

In single level inheritance, there is only one child or derived class from a parent or base class.The example below is a one to one relation between base and a derived class.

class Article:

    def __init__(self, title, content, author):

         self.title = title

         self.content = content

         self.author = author

    def represent(self):

          return f'{self.author}is an author of 

                     ‘{self.title}’

Class Post(Article):

     def __init__(self, title,content,author):

          super().__init__(title,content,author)

art_1 = Article(‘Automate the Boring Stuff’,

                    ‘Practical Programming..’,

                    ‘AL Sweigart’)

art_2= Post(‘Python Tricks’,

                    ‘A Buffet of Awesome Python..’,

                    ‘Dan Bader’,’book.jpg’)

The __init__ method

You might have noticed the __init__ method in the above program. Such prefixed and postfixed double unscored methods are known as magic methods or dunder methods. This method is a constructor or initializer for attributes.A class recognizes its attributes with the self keyword.

The super() method

The super().__init__ method calls the constructor for the Base class with defined parent class attributes i.e., you can use parent class attributes and methods in the Base class.

Passing an argument of class name as base class to a derived class is known as subclassing.By this the derived class identifies its base class.

Multiple Inheritance

In multiple inheritance, a child class has more than one parent class.Here’s an example

class Father:

    def func1(self):

          print(“I am your dad!”)

class Mother:

    def func2(self):

         print(“I am your mom!”)

class Child(Father, Mother):

    def func3(self):

         print(“Hi mom and dad”)

ob = Child()

ob.func1()

ob.func2()

ob.func3()

Multilevel Inheritance

A Multi-level instance is when a derived class is a base class for another class. For instance , like this

class Father:

    def func1(self):

         print(“this is your father”)

class Son(Father):

    def func2(self):

         print(“this is your son”)

class GrandSon(Son):

    def func3(self):

         print(“this is your grandson”)

obj = GrandSon()

obj.func1()

obj.func2()

obj.func3()

Hierarchical Inheritance

Hierarchical Inheritance involves multiple inheritances from the same Parent or Child classes.

class Parent:

        def func1(self):

             print(“I am your Dad”)

class FirstSon(Parent):

        def func2(self):

             print(“I am the eldest brother”)

class SecondSon(Parent):

        def func3(self):

             print(“I am the youngest brother”)

obj1 = FirstSon()

obj2 = SecondSon()

obj1.func1()

obj2.func2()

Hybrid Inheritance

Hybrid Inheritance is when there is multiple and multilevel inheritance in a particular program.

class GrandParent:

        def func1(self):

             print(“I am Your grandpa!”)

class Father(GrandParent):

        def func2(self):

             print(“I am your father!”)

class Uncle(GrandParent):

        def func3(self):

              print(“I am your uncle!”):

class GrandSon(GrandParent , Father):

        def func4(self):

             print(“I am the youngest son.”)

obj = GrandSon()

obj.func1()

This was all about Inheritance and its types, applications and implementation in python.There is a lot more covered about this topic in Online Python Course. More over there is also an on campus version of this course as Python Training in Chennai or Python Training in Bangalore by FITA at an affordable price, which includes real time projects ,certification, support and career guidance assistance.






Quick Enquiry

Please wait while submission in progress...


Contact Us

Chennai

  93450 45466

Bangalore

 93450 45466

Coimbatore

 95978 88270

Online

93450 45466

Madurai

97900 94102

Pondicherry

93635 21112

For Hiring

 93840 47472
 hr@fita.in

Corporate Training

 90036 23340


FITA Academy Branches

Chennai

FITA Academy - Velachery
Plot No 7, 2nd floor,
Vadivelan Nagar,
Velachery Main Road,
Velachery, Chennai - 600042
Tamil Nadu

    :   93450 45466

FITA Academy - Anna Nagar
No 14, Block No, 338, 2nd Ave,
Anna Nagar,
Chennai 600 040, Tamil Nadu
Next to Santhosh Super Market

    :   93450 45466

FITA Academy - T Nagar
05, 5th Floor, Challa Mall,
T Nagar,
Chennai 600 017, Tamil Nadu
Opposite to Pondy Bazaar Globus

    :   93450 45466

FITA Academy - Tambaram
Nehru Nagar, Kadaperi,
GST Road, West Tambaram,
Chennai 600 045, Tamil Nadu
Opposite to Saravana Jewellers Near MEPZ

    :   93450 45466

FITA Academy - Thoraipakkam
5/350, Old Mahabalipuram Road,
Okkiyam Thoraipakkam,
Chennai 600 097, Tamil Nadu
Next to Cognizant Thoraipakkam Office
& Opposite to Nilgris Supermarket

    :   93450 45466

FITA Academy - Porur
17, Trunk Rd,
Porur
Chennai 600116, Tamil Nadu
Above Maharashtra Bank

    :   93450 45466

FITA Academy - Pallikaranai
335A, 13th Main Rd,
Ram Nagar South Extn,
Pallikaranai, Chennai,
Tamil Nadu 600100

    :   93450 45466

Bangalore

FITA Academy Marathahalli
No 7, J J Complex,
ITPB Road, Aswath Nagar,
Marathahalli Post,
Bengaluru 560037

    :   93450 45466

Coimbatore

FITA Academy - Saravanampatty
First Floor, Promenade Tower,
171/2A, Sathy Road, Saravanampatty,
Coimbatore - 641035
Tamil Nadu

    :   95978 88270

FITA Academy - Singanallur
348/1, Kamaraj Road,
Varadharajapuram, Singanallur,
Coimbatore - 641015
Tamil Nadu

    :   95978 88270

Other Locations

FITA Academy - Madurai
No.2A, Sivanandha salai,
Arapalayam Cross Road,
Ponnagaram Colony,
Madurai - 625016, Tamil Nadu

    :   97900 94102

FITA Academy - Pondicherry
410, Villianur Main Rd,
Sithananda Nagar, Nellitope,
Puducherry - 605005
Near IG Square

    :   93635 21112

FITA Academy - Tiruppur
61D, Poongodi Towers 2nd floor,
Periyar Colony Bus Stop,
Tirupur - 641 652

    :   9940122502

Read More Read less
  • Are You Located in Any of these Areas

    Adyar, Adambakkam, Anna Salai, Ambattur, Ashok Nagar, Aminjikarai, Anna Nagar, Besant Nagar, Chromepet, Choolaimedu, Guindy, Egmore, K.K. Nagar, Kodambakkam, Koyambedu, Ekkattuthangal, Kilpauk, Meenambakkam, Medavakkam, Nandanam, Nungambakkam, Madipakkam, Teynampet, Nanganallur, Navalur, Mylapore, Pallavaram, Purasaiwakkam, OMR, Porur, Pallikaranai, Poonamallee, Perambur, Saidapet, Siruseri, St.Thomas Mount, Perungudi, T.Nagar, Sholinganallur, Triplicane, Thoraipakkam, Tambaram, Vadapalani, Valasaravakkam, Villivakkam, Thiruvanmiyur, West Mambalam, Velachery and Virugambakkam.

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur branch is just few kilometre away from your location. If you need the best training in Chennai, driving a couple of extra kilometres is worth it!