Python is an object-oriented programming language that supports inheritance. Inheritance is a mechanism that allows a new class to be based on an existing class. The new class inherits the properties and methods of the existing class, and can also add new properties and methods of its own. Inheritance is a powerful feature of object-oriented programming that allows for code reuse and makes it easier to maintain and extend code.
Python supports multiple inheritance, which means that a class can inherit from more than one parent class. This allows for even greater code reuse and flexibility.
Inheritance is a way to create a new class that is a modified version of an existing class. The existing class is called the parent class, and the new class is called the child class. The child class inherits all the properties and methods of the parent class, and can also add new properties and methods of its own.
Python uses the syntax class ChildClass(ParentClass):
to create a child class that inherits from a parent class. The child class can then use the properties and methods of the parent class, and can also override or add new properties and methods.
Here is an example of a parent class:
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
def speak(self):
print("I am an animal.")
def eat(self):
print("I am eating.")
And here is an example of a child class that inherits from the parent class:
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name, "Dog")
self.breed = breed
def speak(self):
print("Woof!")
def fetch(self):
print("I am fetching.")
In this example, the Dog
class inherits from the Animal
class. The Dog
class overrides the speak
method to make the dog bark instead of saying "I am an animal." The Dog
class also adds a new method called fetch
that the Animal
class does not have.
Here is an example of how to use the Dog
class:
my_dog = Dog("Fido", "Golden Retriever")
print(my_dog.name) # Output: Fido
print(my_dog.species) # Output: Dog
my_dog.speak() # Output: Woof!
my_dog.fetch() # Output: I am fetching.
As you can see, the Dog
class inherits the name
and species
properties from the Animal
class, and also has its own breed
property and fetch
method.
Here are some more examples of how to use inheritance in Python:
Python supports multiple inheritance, which means that a class can inherit from more than one parent class. Here is an example:
class A:
def method_a(self):
print("Method A")
class B:
def method_b(self):
print("Method B")
class C(A, B):
def method_c(self):
print("Method C")
my_object = C()
my_object.method_a() # Output: Method A
my_object.method_b() # Output: Method B
my_object.method_c() # Output: Method C
In this example, the C
class inherits from both the A
and B
classes. The C
class has access to all the methods of both parent classes.
A child class can override a method of its parent class by defining a method with the same name. Here is an example:
class A:
def method(self):
print("Method A")
class B(A):
def method(self):
print("Method B")
my_object = B()
my_object.method() # Output: Method B
In this example, the B
class overrides the method
of the A
class. When the method
is called on an instance of the B
class, the method
of the B
class is called instead of the method
of the A
class.
A child class can call a method of its parent class by using the super()
function. Here is an example:
class A:
def method(self):
print("Method A")
class B(A):
def method(self):
super().method()
print("Method B")
my_object = B()
my_object.method() # Output: Method A
# Method B
In this example, the B
class calls the method
of the A
class using the super()
function. This allows the B
class to add functionality to the method
of the A
class without completely overriding it.