Inheritance in Python
Inheritance is one of the key concepts in object-oriented programming (OOP). In Python, inheritance allows you to define a class that inherits all the methods and properties from another class. The class that is being inherited from is called the base class, and the class that inherits from the base class is called the derived class. In this lesson, we'll explore how to use inheritance in Python and how it can help you write more efficient and maintainable code.
Creating a Derived Class in Python
To create a derived class in Python, you simply need to specify the name of the base class in parentheses after the name of the derived class. The derived class will inherit all the attributes (methods and properties) of the base class, and you can also add your own attributes to the derived class.
class BaseClass:
def __init__(self, name, age):
self.name = name
self.age = age
def get_name(self):
return self.name
def get_age(self):
return self.age
class DerivedClass(BaseClass):
def __init__(self, name, age, gender):
super().__init__(name, age)
self.gender = gender
def get_gender(self):
return self.gender
In this example, the
DerivedClass
is inheriting from the
BaseClass
. The
DerivedClass
has an additional attribute,
gender
, and a method called
get_gender()
, which is not present in the
BaseClass
. However, the
DerivedClass
can still use the methods and attributes of the
BaseClass
, such as
get_name()
and
get_age()
.
Overriding Methods in Python
When you inherit a method from a base class, you can override it in the derived class to provide different functionality. To do this, you simply define a method in the derived class with the same name as the method in the base class. The derived class will use the new implementation of the method instead of the one inherited from the base class.
class BaseClass:
def say_hello(self):
print("Hello from the BaseClass")
class DerivedClass(BaseClass):
def say_hello(self):
print("Hello from the DerivedClass")
In this example, the
DerivedClass
is overriding the
say_hello()
method inherited from the
BaseClass
. When an object of the
DerivedClass
calls
say_hello()
, it will print "Hello from the DerivedClass" instead of "Hello from the BaseClass".
Conclusion
Inheritance is a powerful concept in object-oriented programming that allows you to create new classes based on existing classes. In Python, you can use inheritance to create derived classes that inherit all the methods and properties of a base class, and you can also add your own methods and properties to the derived class. Additionally, you can override methods in the derived class to provide different functionality.
In this lesson, we've covered the basics of inheritance in Python. In the next lessons, we'll explore more advanced topics, such as OOP concepts like polymorphism and encapsulation, iterators, generators and closures. By understanding inheritance and these other advanced concepts, you'll be able to write more efficient, maintainable, and powerful Python code.