Python

 

 

Introduction

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which contain both data and methods. Python, being an object-oriented language, allows you to organize your code into reusable and modular components. OOP promotes code reusability, scalability, and maintainability, making it an essential concept for programmers.

Objectives of the Topic

  • Understand the key concepts of Object-Oriented Programming (OOP).
  • Learn how to define classes and objects in Python.
  • Understand how to implement inheritance, encapsulation, and polymorphism in Python.
  • Explore practical examples to solidify your understanding of OOP in Python.

Example Code

Program to demonstrate the basic concepts of OOP in Python

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def speak(self):
        return f"{self.name} makes a sound."

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name, species="Dog")
        self.breed = breed

    def speak(self):
        return f"{self.name} says Woof!"

class Cat(Animal):
    def __init__(self, name, breed):
        super().__init__(name, species="Cat")
        self.breed = breed

    def speak(self):
        return f"{self.name} says Meow!"

# Creating objects
dog = Dog("Buddy", "Golden Retriever")
cat = Cat("Whiskers", "Siamese")

# Displaying the output
print(dog.speak())
print(cat.speak())

Explanation of the Program Structure

The program consists of three classes: Animal, Dog, and Cat.

  • Animal class: This is the base class with an initializer method __init__ that takes name and species as arguments. The speak method is defined to return a generic message.
  • Dog and Cat classes: These classes inherit from the Animal class. They override the speak method to provide species-specific behavior (Woof for dogs, Meow for cats).
  • Creating Objects: Instances of the Dog and Cat classes are created using their respective constructors, and then the speak method is called on both objects to demonstrate polymorphism in action.

How to Run the Program

To run this program:

  1. Copy the code into a text editor or an IDE (like PyCharm, Visual Studio Code, or Jupyter Notebook).
  2. Save the file with a .py extension, for example, oop_example.py.
  3. Open a terminal or command prompt and navigate to the directory where the file is saved.
  4. Run the program using the command: python oop_example.py.
  5. You will see the output, where the speak method of both objects is printed.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)