Python Inheritance

← Back to Home

 

๐Ÿ Inheritance in Python (Made Simple)

Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows a class to use the properties and methods of another class.

Inheritance allows us to define a class that inherits all the methods and properties from another class.


Think of it like a family: a child can inherit features from their parents.

Parent class is the class being inherited from, also called base class.

Child class is the class that inherits from another class, also called derived class.

 In programming, a class (child) can inherit features (methods and variables) from another class (parent).

For more on Python classes, check out our post on Python Classes and Objects.


✅ Why Use Inheritance?

  • To reuse code.

  • To make the program easier to manage.

  • To follow the DRY principle (Don't Repeat Yourself).


๐Ÿงฑ Basic Syntax

class Parent:
    # parent class
    pass

class Child(Parent):
    # child class that inherits from Parent
    pass

๐Ÿ“˜ Example: Simple Inheritance

# Parent class
class Animal:
    def speak(self):
        print("I am an animal")

# Child class
class Dog(Animal):
    def bark(self):
        print("Woof!")

# Creating object of Dog
my_dog = Dog()

my_dog.speak()  # Inherited from Animal
my_dog.bark()   # Defined in Dog

๐Ÿ” Output:

I am an animal
Woof!

Here, the Dog class inherits the speak() method from the Animal class. It also has its own method called bark().


๐Ÿ“˜ Example 2: Inheriting from a Person Class

Let’s create a simple example where a Student class inherits from a Person class.

๐Ÿ‘จ‍๐Ÿ‘ฉ‍๐Ÿ‘ง‍๐Ÿ‘ฆ Step 1: Create the Parent Class

# Parent class
class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

# Creating an object of Person
x = Person("Steve", "Tendulkar")
x.printname()

๐Ÿ” Output:

Steve Tendulkar

๐Ÿง’ Step 2: Create the Child Class

# Child class that inherits from Person
class Student(Person):
    pass  # No additional code, just inheriting everything from Person

# Creating an object of Student
x = Student("Mike", "Olsen")
x.printname()

๐Ÿ” Output:

Mike Olsen

๐Ÿ“ Note:
The pass keyword is used when you don't want to add any extra functionality to the child class. The Student class automatically gets all the properties and methods from the Person class.



๐Ÿ”„ Types of Inheritance in Python

  1. Single Inheritance – One child inherits from one parent.

  2. Multiple Inheritance – One child inherits from multiple parents.

  3. Multilevel Inheritance – A child inherits from a parent, and another child inherits from that child.

  4. Hierarchical Inheritance – Multiple children inherit from one parent.

  5. Hybrid Inheritance – A mix of any of the above types.


๐Ÿงช Example: Multiple Inheritance

class Father:
    def skill(self):
        print("Good at driving")

class Mother:
    def talent(self):
        print("Good at cooking")

class Child(Father, Mother):
    def hobby(self):
        print("Loves painting")

c = Child()
c.skill()
c.talent()
c.hobby()

๐Ÿ” Output:

Good at driving
Good at cooking
Loves painting

๐Ÿ’ก Important Points

  • The super() function is used to call methods from the parent class.

  • Inheritance makes your code organized and reusable.

  • If both parent and child have the same method, the child’s method will override the parent’s method.


๐Ÿ›  Using super() Example

class Animal:
    def speak(self):
        print("Animal speaks")

class Dog(Animal):
    def speak(self):
        super().speak()
        print("Dog barks")

d = Dog()
d.speak()

๐Ÿ” Output:

Animal speaks
Dog barks

๐ŸŽฏ Conclusion

Inheritance helps us write cleaner and more efficient code. Once we understand it, we can build complex applications more easily by reusing and extending existing code.

For more on Python classes, check out our post on Python Classes and Objects.

No comments:

Post a Comment

Featured Post

Add Keyboard Shortcuts to Tkinter GUI – Python Hotkeys with Bind Examples

  ← Back to Home ⌨️ Bonus Part 5: Keyboard Shortcuts & Hotkeys in Tkinter – Boost App Productivity ๐Ÿ“˜ Overview Keyboard shortcuts ...

Popular Posts