Understanding Object Oriented Programming in Python
Introduction
Object-Oriented Programming (OOP) is one of the most prominent paradigms in programming, used by developers across various industries to build resourceful applications. OOP is a programming model that is centered around the concept of "objects", which are instances of classes that can contain data and functions to manipulate that data.
Although OOP provides many benefits to developers, it can be a challenging concept for newbies to grasp. One of the fundamental concepts of OOP is inheritance, which allows developers to create new classes based on existing ones, inheriting all the properties and methods of the parent class.
Understanding inheritance is crucial for building efficient and maintainable code. With proper knowledge of inheritance, a developer can avoid writing repetitive code and create complex applications with ease. However, without a proper understanding of inheritance and other OOP concepts, a developer can end up with bloated and unmanageable code that is difficult to maintain.
In this article, we will explore inheritance in OOP in detail. By the end of this article, you will have a solid understanding of inheritance in OOP and be able to apply this knowledge to build efficient and maintainable code in your real-life applications.
Inheritance
Inheritance is a concept in OOP that allow classes to inherit properties from another class. This help to build building testable, efficient and maintainable code. Let's dive into it
I will start by writing a simple class in python
class Animal:
# This is going to be our init funtion where we initialize our properties
def __init__(self, name, size):
self._name = name
self._size = size
# Let's write some methods
def make_sound(self, sound):
print(sound);
def move(self):
print('Animal is moving')
animal = Animal('Dog', 'medium')
print(animal._name) # Output: Dog
print(animal._size) # Output: medium
animal.make_sound('barked barked') # Output: barked barked
animal.move() # Output: Animal is moving
We created a parent class that future children classes can inherit from. The children's class can inherit the properties like the name
, size,
and also the methods like make_sound
, move
. Let's look at how we will inherit from the Animal class in the Dog class
class Dog(Animal):
# This is going to be our init function where we initialize our properties
def __init__(self, name, size, breed):
super().__init__(name, size)
self._breed = breed
def make_sound(self):
print('Dog is moving on four feet')
my_dog = Dog('Fido', 'medium', 'Labrador')
print(my_dog._name) # Output: Fido
print(my_dog._size) # Output: medium
print(my_dog._breed) # Output: Labrador
my_dog. make_sound('Bark bark!') # Output: Bark bark!
my_dog.move() # Output: Dog is moving on four feet
Let me Explain the Entire Code
In object-oriented programming, inheritance is a way of creating a new class based on an existing class. The new class is called the child class or subclass, and the existing class is called the parent class or superclass.
In our animal analogy, the Animal class is the parent class, and the Dog class is the child class. When we define the Dog class like this:
class Dog(Animal):
We're saying that the Dog class should inherit all the properties and methods of the Animal class. This means that the Dog class automatically gets access to the name
and size
properties and the make_sound
and move
methods defined in the Animal class.
When we create a new instance of the Dog class, like this:
my_dog = Dog('Fido', 'medium', 'Labrador')
We're actually creating a new object that has all the properties and methods of both the Dog and Animal classes. This is possible because the Dog class inherits from the Animal class.
We can see that the Dog class has access to the properties of the Animal class by looking at the __init__
method of the Dog class:
def __init__(self, name, size, breed):
super().__init__(name, size)
self._breed = breed
Here, we're calling the __init__
method of the parent class using the super()
function, which initializes the name
and size
properties of the Animal class in the Dog instance.
In addition to inheriting properties, the Dog class can also override methods that it inherits from the Animal class. This means that the Dog class can change the behavior of a method that it inherits from the Animal class to better suit the needs of a dog.
For example, we've overridden the move
method in the Dog class to provide more specific behavior for dogs:
def move(self):
print('Dog is moving on four feet')
This means that when we call the move
method on a Dog instance, it will output Dog is moving on four feet
instead of the generic Animal is moving
output of the Animal class.
In summary, inheritance is a powerful feature of object-oriented programming that allows you to create new classes based on existing classes, inheriting their properties and methods. This makes it easier to write reusable code and to create new classes that build on the functionality of existing classes.