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, Bangalore & Online

  93450 45466

Coimbatore

 95978 88270

Tirupur

99401 22502

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

    Adambakkam, Adyar, Akkarai, Alandur, Alapakkam, Alwarpet, Alwarthirunagar, Ambattur, Ambattur Industrial Estate, Aminjikarai, Anakaputhur, Anna Nagar, Anna Salai, Arumbakkam, Ashok Nagar, Avadi, Ayanavaram, Besant Nagar, Bharathi Nagar, Camp Road, Cenotaph Road, Central, Chetpet, Chintadripet, Chitlapakkam, Chengalpattu, Choolaimedu, Chromepet, CIT Nagar, ECR, Eechankaranai, Egattur, Egmore, Ekkatuthangal, Gerugambakkam, Gopalapuram, Guduvanchery, Guindy, Injambakkam, Irumbuliyur, Iyyappanthangal, Jafferkhanpet, Jalladianpet, Kanathur, Kanchipuram, Kandhanchavadi, Kandigai, Karapakkam, Kasturbai Nagar, Kattankulathur, Kattupakkam, Kazhipattur, Keelkattalai, Kelambakkam, Kilpauk, KK Nagar, Kodambakkam, Kolapakkam, Kolathur, Kottivakkam, Kotturpuram, Kovalam, Kovilambakkam, Kovilanchery, Koyambedu, Kumananchavadi, Kundrathur, Little Mount, Madambakkam, Madhavaram, Madipakkam, Maduravoyal, Mahabalipuram, Mambakkam, Manapakkam, Mandaveli, Mangadu, Mannivakkam, Maraimalai Nagar, Medavakkam, Meenambakkam, Mogappair, Moolakadai, Moulivakkam, Mount Road, MRC Nagar, Mudichur, Mugalivakkam, Muttukadu, Mylapore, Nandambakkam, Nandanam, Nanganallur, Nanmangalam, Narayanapuram, Navalur, Neelankarai, Nesapakkam, Nolambur, Nungambakkam, OMR, Oragadam, Ottiyambakkam, Padappai, Padi, Padur, Palavakkam, Pallavan Salai, Pallavaram, Pallikaranai, Pammal, Parangimalai, Paruthipattu, Pazhavanthangal, Perambur, Perumbakkam, Perungudi, Polichalur, Pondy Bazaar, Ponmar, Poonamallee, Porur, Pudupakkam, Pudupet, Purasaiwakkam, Puzhuthivakkam, RA Puram, Rajakilpakkam, Ramapuram, Red Hills, Royapettah, Saidapet, Saidapet East, Saligramam, Sanatorium, Santhome, Santhosapuram, Selaiyur, Sembakkam, Semmanjeri, Shenoy Nagar, Sholinganallur, Singaperumal Koil, Siruseri, Sithalapakkam, Srinivasa Nagar, St Thomas Mount, T Nagar, Tambaram, Tambaram East, Taramani, Teynampet, Thalambur, Thirumangalam, Thirumazhisai, Thiruneermalai, Thiruvallur, Thiruvanmiyur, Thiruverkadu, Thiruvottiyur, Thoraipakkam, Thousand Light, Tidel Park, Tiruvallur, Triplicane, TTK Road, Ullagaram, Urapakkam, Uthandi, Vadapalani, Vadapalani East, Valasaravakkam, Vallalar Nagar, Valluvar Kottam, Vanagaram, Vandalur, Vasanta Nagar, Velachery, Vengaivasal, Vepery, Vettuvankeni, Vijaya Nagar, Villivakkam, Virugambakkam, West Mambalam, West Saidapet

    FITA Velachery or T Nagar or Thoraipakkam OMR or Anna Nagar or Tambaram or Porur or Pallikaranai 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!