Object-Oriented Programming (OOP) is a programming paradigm based on the concept of 'objects', which can contain data (attributes) and code (methods). The primary goal of OOP is to organize code into reusable, modular units, making programs easier to design, implement, and maintain. The core principles of OOP are Encapsulation, Abstraction, Inheritance, and Polymorphism, though this explanation focuses on Classes, Inheritance, and Polymorphism.
1. Classes:
A class is a blueprint or a template for creating objects. It defines the structure (attributes/data) and behavior (methods/functions) that objects of that class will have. Think of a class as a cookie cutter; it doesn't represent an actual cookie but defines how cookies made with it will look. Once a class is defined, you can create multiple 'objects' or 'instances' from it. Each object is a concrete entity based on the class, having its own unique set of attribute values.
- Attributes: These are variables defined within a class that store data. They represent the characteristics or properties of an object.
- Methods: These are functions defined within a class that perform actions or operate on the object's attributes. They represent the behaviors an object can perform.
2. Inheritance:
Inheritance is a mechanism that allows a new class (called a subclass or child class) to derive properties (attributes and methods) from an existing class (called a superclass or parent class). This concept promotes code reusability and establishes an 'is-a' relationship (e.g., a 'Dog' -is an- 'Animal'). The child class can extend the functionalities of the parent class by adding new attributes or methods, or it can modify (override) existing methods of the parent class.
- Parent/Base Class: The class from which other classes inherit.
- Child/Derived Class: The class that inherits from a parent class. It can add new features or customize inherited ones.
3. Polymorphism:
Polymorphism means 'many forms'. In OOP, it refers to the ability of different objects to respond to the same message (method call) in a way that is specific to their own class, even if they share a common interface or are treated as objects of a common type. The most common form of polymorphism in OOP is method overriding.
- Method Overriding: A specific form of polymorphism where a subclass provides its own implementation of a method that is already defined in its superclass. When this method is called on an object, the appropriate version (from the superclass or subclass) is executed based on the actual type of the object at runtime.
Example Code
python
1. Class Definition
class Animal:
def __init__(self, name):
Attributes
self.name = name
Method
def speak(self):
return f"{self.name} makes a sound."
def introduce(self):
return f"Hello, I am {self.name}."
2. Inheritance
Dog class inherits from Animal
class Dog(Animal):
def __init__(self, name, breed):
Call the parent class's constructor
super().__init__(name)
self.breed = breed
Method Overriding (Polymorphism in action)
def speak(self):
return f"{self.name} barks! Woof woof!"
Adding a new method specific to Dog
def fetch(self):
return f"{self.name} is fetching the ball."
Cat class inherits from Animal
class Cat(Animal):
def __init__(self, name, color):
super().__init__(name)
self.color = color
Method Overriding
def speak(self):
return f"{self.name} meows! Meow!"
def purr(self):
return f"{self.name} is purring."
Creating objects (instances) from classes
my_animal = Animal("Generic Animal")
my_dog = Dog("Buddy", "Golden Retriever")
my_cat = Cat("Whiskers", "Tabby")
print(my_animal.introduce()) Output: Hello, I am Generic Animal.
print(my_animal.speak()) Output: Generic Animal makes a sound.
print("---------------------")
print(my_dog.introduce()) Inherited method
print(my_dog.speak()) Overridden method
print(my_dog.fetch()) New method for Dog
print(f"Buddy's breed: {my_dog.breed}")
print("---------------------")
print(my_cat.introduce()) Inherited method
print(my_cat.speak()) Overridden method
print(my_cat.purr()) New method for Cat
print(f"Whiskers' color: {my_cat.color}")
print("---------------------")
3. Polymorphism in action: Treating objects of different classes uniformly
animals = [my_animal, my_dog, my_cat]
print("Demonstrating Polymorphism:")
for animal in animals:
The 'speak' method behaves differently based on the actual object type
print(animal.speak())
Output:
Generic Animal makes a sound.
Buddy barks! Woof woof!
Whiskers meows! Meow!








Object-Oriented Programming (OOP: Classes, Inheritance, Polymorphism)