Home » Posts tagged 'Object-oriented programming in Python'
Tag Archives: Object-oriented programming in Python
Python Inheritance
In this tutorial we, are going to learn Inheritance concept.
Inheritance is an important concept in object-oriented programming (OOP) that allows you to create new classes (derived classes) based on existing classes (base or parent classes). It is a way to establish a hierarchical relationship between classes, where the derived classes inherit the properties and behaviors of the base class.
The class from which a new class inherits is called the “base class” or “superclass,” and the class that inherits from the base class is called the “derived class” or “subclass.” Inheritance facilitates code reuse and the creation of hierarchical relationships between classes.
To define a class that inherits from another class, you specify the base class name in parentheses after the subclass name in the class definition. Here’s a basic syntax example
In Python, you can implement inheritance using the following syntax:
class BaseClass:
# Base class attributes and methods
class DerivedClass(BaseClass):
# Derived class attributes and methods
Here, DerivedClass
is the derived class that inherits from BaseClass
. The derived class inherits all the attributes (variables) and methods (functions) from the base class. It can also add new attributes or override existing methods from the base class to customize its behavior.
Let’s see an example:
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
print("Animal speaks.")
class Dog(Animal):
def __init__(self, name):
super().__init__(name)
def speak(self):
print("Dog barks.")
class Cat(Animal):
def __init__(self, name):
super().__init__(name)
def speak(self):
print("Cat meows.")
# Creating instances of derived classes
dog = Dog("Buddy")
dog.speak() # Output: Dog barks.
cat = Cat("Whiskers")
cat.speak() # Output: Cat meows.
In this example, we have a base class Animal
that has an attribute name
and a method speak()
. The derived classes Dog
and Cat
inherit from Animal
. They both have their own __init__
method to initialize the name
attribute, and they override the speak()
method to provide their specific behavior.
When we create instances of Dog
and Cat
and call their speak()
method, the appropriate implementation of the method in the derived class is executed.
Inheritance helps in code reuse, as you can define common attributes and methods in the base class and then extend or specialize them in the derived classes as needed. It promotes code organization, modularity, and flexibility in your program’s design.
Python OOPs Concepts
Python supports object-oriented programming (OOP) concepts. Here are the main concepts in Python OOP:
1. Classes and Objects: A class is a blueprint for creating objects, while an object is an instance of a class. Classes define the properties (attributes) and behaviors (methods) that objects of that class can have.
2. Encapsulation: Encapsulation is the process of bundling data and methods within a class, hiding the internal details and protecting the data from direct manipulation. It is achieved by using access modifiers like public, private, and protected.
3. Inheritance: Inheritance allows a class to inherit properties and methods from another class. The class being inherited from is called the base class or superclass, and the class inheriting from it is called the derived class or subclass. The derived class can extend or override the functionality of the base class.
4. Polymorphism: Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables methods with the same name to be called on different objects, and the appropriate method is executed based on the object’s type. Polymorphism can be achieved through method overriding and method overloading.
5. Method Overriding: Method overriding occurs when a derived class defines a method with the same name as a method in its superclass. The derived class’s method overrides the implementation of the base class method, allowing the derived class to provide its own implementation.
6. Method Overloading: Method overloading enables a class to have multiple methods with the same name but different parameters. Python does not support method overloading directly, but you can achieve similar behavior using default argument values or variable-length argument lists.
7. Abstraction: Abstraction refers to the process of hiding complex implementation details and providing a simplified interface. It allows the user to interact with the objects without needing to know the underlying complexity. Abstract classes and interfaces are used to achieve abstraction in Python.
8. Composition: Composition is a form of association where one class contains an instance of another class as a member. It allows creating complex objects by combining simpler objects. Composition is often used when the relationship between classes is not a “is-a” relationship (as in inheritance) but rather a “has-a” relationship.
These concepts form the foundation of object-oriented programming in Python. By utilizing these concepts, you can create modular, reusable, and maintainable code that models real-world entities effectively.