Tuesday 12 March 2024

what is object oriented programming

 What is Object-Oriented Programming (OOP)?

OOP is a programming paradigm (a way of thinking about and structuring your code) centered around the concept of "objects." Here's the basic breakdown:

  • Objects: Objects are self-contained bundles of data (attributes) and code that operates on that data (methods). Think of them as blueprints for creating real-world entities.

  • Example: A "car" object might have attributes like color, model, and mileage, and methods like start(), stop(), and accelerate().

  • Classes: Classes act as the blueprints for creating objects. They define the attributes and methods that objects of that type will possess.

Key Principles of OOP

  1. Encapsulation: This is all about bundling data and the relevant code that works on that data together within an object. It helps keep things organized and protects data from unintended modification by external code.

  2. Abstraction: The idea of exposing only the essential details and hiding the internal implementation. This simplifies using objects – you interact with them through well-defined methods without needing to know exactly how they do their thing.

  3. Inheritance: This allows you to create new classes (child classes) that inherit properties and behaviors from existing classes (parent classes). This promotes code reuse and helps define hierarchical relationships between things.

  4. Polymorphism: Means "many forms." It refers to the ability of objects of different classes to respond to the same method in a way that's appropriate to their class. This makes code more flexible and adaptable.

Benefits of OOP

  • Code Reusability: Classes act as blueprints, saving you tons of time and effort.

  • Modularity: Objects are self-contained units, making code more organized and easier to understand.

  • Maintainability: Changes to one object have minimal impact on others, making your code easier to change and troubleshoot.

  • Real-world modeling: OOP helps you model software according to real-world concepts, making design more intuitive.

Example (Simplified)


class Dog:
    def __init__(self, name, breed):  # Constructor = name
        self.breed = breed

    def bark(self):

# Create an object
my_dog = Dog("Buddy", "Golden Retriever"
my_dog.bark() # Output: Woof!


Let's break down the concepts of objects, classes, attributes, and methods in object-oriented programming with examples.


  • What: An object is an instance of a class. It's a self-contained bundle of data (attributes) and the procedures that operate on that data (methods). Objects represent real-world entities or concepts within your program.

  • Example:

  • A Car object: It would have attributes like color, make, model, and methods like start(), accelerate(), and brake().


  • What: A class is a blueprint or template for creating objects. It defines the structure and behavior that all objects of that type will share.

  • Example:

  • A Car class: It defines the attributes and methods that any Car object should have.


  • What: Attributes are variables that hold the data associated with an object. They represent an object's characteristics or state.

  • Example:

  • In a Car class, attributes could be color, make, model, mileage, etc.


  • What: Methods are functions defined within a class. They represent the behaviors or actions that an object can perform.

  • Example:

  • In a Car class, methods could be start(), accelerate(), brake(), turn(), etc.

Example with Python Code


class Dog:
    def __init__(self, name, breed):  # Constructor to initialize attributes = name
        self.breed = breed

    def bark(self):  # Method representing a behavior

# Create a Dog object
my_dog = Dog("Fido", "Labrador")

# Accessing attributes
print(  # Output: Fido
print(my_dog.breed)  # Output: Labrador

# Calling a method
my_dog.bark()  # Output: Woof!

Key Points

  • Classes provide the template; objects are the real-world instances created from that template.

  • Attributes store the data specific to each object.

  • Methods define the actions an object can take.


Difference between method and function


  • Part of a Class: A method is a function that is defined inside a class.

  • Object Association: Methods are intrinsically tied to objects of the class they belong to. They operate on the data (attributes) of that specific object.

  • The 'self' Keyword: Methods usually have the self parameter (or similar conventions in other languages), which refers to the object itself.

  • Example:
    class Dog:
        def bark(self):  # 'self' refers to the specific Dog object

    my_dog = Dog()
    my_dog.bark()  # Calls the bark method on the 'my_dog' object


  • Standalone: Functions are independent blocks of code that don't belong to a specific class.

  • Data as Parameters: Functions primarily work with data that is passed to them as arguments (parameters). They don't automatically have access to an object's attributes.

  • Global Scope: Functions can often be defined anywhere and used from different parts of your program.

  • Example:
    def greet(name):
        print("Hello, " + name + "!")


Table Summary





Bound to an object of a class



Object's data and behavior

Performing a specific task

Access to Data

Accesses the object's attributes (via self)

Relies on passed arguments

Key takeaway: In OOP, methods are a way to organize and structure the behavior associated with objects, while functions provide independent, reusable units of code.


Methods for Lists (Mutable Sequences)

  • append(item): Adds an item to the end of the list.
    numbers = [1, 2, 3]
    print(numbers)  # Output: [1, 2, 3, 4]

  • insert(index, item): Inserts an item at a specified index in the list.
    numbers.insert(1, 10)
    print(numbers)  # Output: [1, 10, 2, 3, 4]

  • sort(): Sorts the elements of the list in ascending order.
    letters = ['c', 'a', 'd', 'b']
    print(letters)  # Output: ['a', 'b', 'c', 'd']

  • reverse(): Reverses the order of the elements in the list.
    print(letters)  # Output: ['d', 'c', 'b', 'a']

  • count(item): Returns the number of times an item appears in the list.
    numbers = [1, 5, 2, 1, 5]
    print(numbers.count(5))  # Output: 2

  • index(item): Returns the index of the first occurrence of an item in the list.
    print(numbers.index(1))  # Output: 0

Methods for Strings (Which are Sequences of Characters)

  • upper(): Returns a copy of the string with all letters converted to uppercase.
    text = "hello world"
    print(text.upper())  # Output: HELLO WORLD

  • lower(): Returns a copy of the string with all letters converted to lowercase.

  • find(sub): Returns the index of the first occurrence of the substring 'sub' within the string.

  • replace(old, new): Returns a copy of the string where all occurrences of 'old' are replaced with 'new'.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.