Inheritance allows us to define a class that inherits all the methods and properties from another class. 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.
When a child class inherits from a parent class, it automatically gains access to all the attributes and methods of the parent class. The child class can then add new attributes and methods, or modify the behavior of existing methods.
Inheritance allows us to reuse code and create classes that are more specialized than their parent classes. We can also create complex inheritance hierarchies with multiple levels of inheritance.
Here's a simple example of inheritance in Python:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError("Subclass must implement this abstract method")
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow"
my_dog = Dog("Rufus")
print(my_dog.name) # Output: Rufus
print(my_dog.speak()) # Output: Woof!
my_cat = Cat("Fluffy")
print(my_cat.name) # Output: Fluffy
print(my_cat.speak()) # Output: Meow
Inheritance is the mechanism that allows us to create a hierarchy of classes that share a set of variables and methods by deriving it from another class. It is one of the core concepts in Object-Oriented Programming.
Every class in Python inherits from a built-in basic class called 'object'. The constructor, ie. the
__init__() function of a class is invoked when we create an instance of that class. The variables that are described within
__init__() are called the instance variables of the class.
# Base Class (inherits from object)
class Animal(object):
def __init__(self,name,type): # constructor
self.name = name
self.type = type
def display(self):
print(self.name, self.type)
# Derived Class (inherits from Animal)
class Cat(Animal):
def display2:
print("This is inside derived class.")
a = Animal("animal","organism") # instance of base class
a.display()
c = Cat("cat","mammal") # instance of derived class
c.display()
c.display2()
# output = animal organism
cat mammal
This is inside derived class.
There are two types of inheritance in Python: 1. Single inheritance: This is when the child class inherits from only one parent class, an example of which was shown above.
2. Multiple inheritances: This is when the child class inherits from more than one parent class. Example:
# 1st Base Class
class Class1(object):
def __init__(self,name): # constructor
self.name = name
def display(self):
print(self.name)
# 2nd Base Class
class Class2(object):
def __init__(self,num): # constructor
self.num = num
def square(self):
print(self.num**2)
class DerivedClass(Class1, Class2):
def __init__(self,name,num):
Class1.__init__(self,name) # invoking constructor of 1st base class
Class2.__init__(self,num) # invoking constructor of 2nd base class
d = DerivedClass("John",5)
d.display()
d.square()
# output = John
25
3. Multilevel inheritance: This is when the child class inherits from a parent class, and another class inherits from that child class
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Inheritance allows us to define a class that inherits all the methods and properties from another class. 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.
When a child class inherits from a parent class, it automatically gains access to all the attributes and methods of the parent class. The child class can then add new attributes and methods, or modify the behavior of existing methods.
Inheritance allows us to reuse code and create classes that are more specialized than their parent classes. We can also create complex inheritance hierarchies with multiple levels of inheritance.
Here's a simple example of inheritance in Python:
Inheritance is the mechanism that allows us to create a hierarchy of classes that share a set of variables and methods by deriving it from another class. It is one of the core concepts in Object-Oriented Programming.
Every class in Python inherits from a built-in basic class called 'object'. The constructor, ie. the __init__() function of a class is invoked when we create an instance of that class. The variables that are described within __init__() are called the instance variables of the class.
There are two types of inheritance in Python:
1. Single inheritance: This is when the child class inherits from only one parent class, an example of which was shown above.
2. Multiple inheritances: This is when the child class inherits from more than one parent class. Example:
3. Multilevel inheritance: This is when the child class inherits from a parent class, and another class inherits from that child class