Categories: python

Inheritance in Python

Inheritance is a fundamental idea in object-oriented programming (OOP) languages. By deriving a class from another class, you can use this mechanism to build a hierarchy of classes that share a set of properties and methods.

Benefits of inheritance include:
  • It accurately depicts real-world relationships.
  • It offers a code’s reusability. We don’t need to keep writing the same code. Additionally, it enables us to expand a class’s features without changing them.
  • Because of its transitive nature, if class B inherits from class A, then all of class B’s subclasses will also automatically inherit from class A.
  • A clear, understandable model structure is provided by inheritance.
  • An inheritance reduces the cost of development and maintenance.
Syntax for Inheritance:
Class BaseClass:
    Body
Class DerivedClass(BaseClass):
    Body
Example:

class Person(object):

 # Constructor
 def __init__(self, name):
  self.name = name

 # To get name
 def getName(self):
  return self.name

 # To check if this person is an employee
 def isStudent(self):
  return False


# Inherited or Subclass (Note Person in bracket)
class Student(Person):

 # Here we return true
 def isStudent(self):
  return True


# Driver code
obj = Person("Mayank") # An Object of Person
print(obj.getName(), obj.isStudent())

obj = Student("Shital") # An Object of Employee
print(obj.getName(), obj.isStudent())
Output:
Mayank False
Shital True
Using the Child Class to Access the Parent Class Element:

You might occasionally need to use the functions or properties of the parent class while working in a child class. The dot. operator can be used to access the elements of a parent class.

Example:

Parent.variableName

We have a straightforward example to illustrate this below:

class Parent:
   var1 = 1
   def func1(self):
       # do something here

class Child(Parent):
   var2 = 2
   def func2(self):
        # do something here too
    # time to use var1 from 'Parent'
       myVar = Parent.var1 + 10
       return myVar

Note: also read about Destructors in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago