Introduction to Object-Oriented Programming (OOP)

Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP), is a way of writing programs by focusing on real-life objects. In the real world, everything we deal with is an object, such as a car, a book, or a student. Each of these objects has certain features and can perform specific actions. In OOP, we try to represent these features as attributes and the actions as functions.

This programming style helps us organize our code in a way that is closer to how we understand things in real life. It makes large programs easier to design, manage, and extend.

Aspect comparison

AspectProcedural ProgrammingObject-Oriented Programming (OOP)
ApproachTop-downBottom-up
StructureProgram is divided into functionsProgram is divided into objects and classes
Data & FunctionsData and functions are separateData and functions are bundled together in objects
ReusabilityLimitedHigh (through inheritance and modularity)
SecurityLess secure (data is exposed)More secure (data hidden using encapsulation)
Real-world ModelingDifficult to model real-world scenariosEasier to model real-world entities and interactions
ExamplesC, PascalJava, C++, Python (with OOP), C#
Function ReuseUses functions repeatedlyUses objects and methods repeatedly
Code MaintenanceHarder to manage and scale in large programsEasier to manage due to modular structure
Main FocusFocuses on functions (actions)Focuses on objects (entities)

What is OOP?

Object-oriented programming is a programming model that allows developers to organize and build programs around real-world concepts. In this model, we define objects that contain both data (attributes) and actions (functions or methods).

The main concepts of OOP are:

  • Classes
  • Objects
  • Attributes
  • Functions
  • Encapsulation
  • Inheritance
  • Polymorphism

Each of these will be explained step-by-step below.

Why Use Object-Oriented Programming (OOP)?

Reusability: Through inheritance, new classes can be created based on existing ones, inheriting their properties and methods. This means you can reuse code from previously developed classes, saving time and effort.

Modularity: OOP breaks down complex problems into smaller, manageable objects. Each object encapsulates its data and behavior, making it easier to understand, modify, and test independently.

Maintainability: Encapsulation helps isolate changes, reducing unintended side effects and making updates safer over time.

Flexibility: Polymorphism allows objects of different classes to be treated uniformly if they share the required interface, increasing adaptability.

Key Concepts in Object-Oriented Programming (OOP)

Class

Class is the template, design or blueprint for creating real-world objects. It defines what attributes and functions an object will have, but it doesn’t store real data itself.

Representation of class in OOP

For example, a Student class can define:

  • Attributes like name, rollNo, and department
  • Functions like giveExam() and submitAssignment()

You can then create many student objects from this one class.

Object

An object is a real instance of a class. It is created using the blueprint defined by the class and stores actual data.

Class and its objects in OOP

For example, once you define a Student class, you can create:

// C++ example
#include <string>

class Student {
public:
    std::string name;
    void giveExam() {
        // ...
    }
};

int main() {
    Student s1;
    s1.name = "Ali";
    s1.giveExam();
    return 0;
}

Here, s1 is an object of class Student. Each object can have different values for the same attributes, like different names or roll numbers.

Attributes

Attributes are the properties or characteristics of an object (also called data members). They describe what an object has.

Object attributes in OOP

Attributes are written inside a class and accessed through objects.

For example:

// C++ example
#include <string>

class Book {
public:
    std::string title;
    std::string author;
    int pages;
};

Here, title, author, and pages are attributes. Each book object will have its own values for these.

Functions

Functions (methods) define the actions an object can perform. They are written inside the class and called using the object.

Function of the object in OOP

For example:

// C++ example
#include <iostream>

class Book {
public:
    void read() {
        std::cout << "Reading the book..." << '\n';
    }

    void bookmarkPage() {
        std::cout << "Page bookmarked." << '\n';
    }
};

When we create a Book object, it can call these functions to perform tasks.

Encapsulation

Encapsulation means protecting the internal data of an object by keeping attributes private and accessing them only through special functions (getters and setters).

Encapsulation in OOP

This improves security and keeps the data safe from direct changes.

Example:

// C++ example
class Student {
private:
    int marks;

public:
    int getMarks() const {
        return marks;
    }

    void setMarks(int m) {
        marks = m;
    }
};

Here, marks is private, so it cannot be accessed directly.

Inheritance

Inheritance means creating a new class based on an existing class. The new class (child) can use everything from the old class (parent) and also add its own features.

Inheritance in OOP

Example:

// C++ example
#include <iostream>

class Vehicle {
public:
    void start() {
        std::cout << "Starting..." << '\n';
    }
};

class Car : public Vehicle {
public:
    void playMusic() {
        std::cout << "Playing music..." << '\n';
    }
};

Car is using start() from Vehicle and also has its own function playMusic().

Polymorphism

Polymorphism means one function behaves differently for different classes. It allows the same function name to do different jobs based on the object.

Polymorphism in OOP

Example:

// C++ example
#include <iostream>

class Animal {
public:
    virtual void speak() {
        std::cout << "Animal speaks" << '\n';
    }
    virtual ~Animal() = default;
};

class Dog : public Animal {
public:
    void speak() override {
        std::cout << "Dog barks" << '\n';
    }
};

When we call speak(), the result depends on whether the object is an Animal or Dog.

Task

Take a Book as an example. Now apply OOP concepts:

  • Object: A specific book like book1
  • Attributes: title, author, price
  • Functions: read(), bookmarkPage()
  • Encapsulation: Keep price private, use methods to get/set it
  • Inheritance: Make EBook a child class of Book
  • Polymorphism: Override read() in EBook and PrintedBook

12 thoughts on “Introduction to Object-Oriented Programming (OOP)

  1. Celebrate life’s special moments with affordable luxury party buses in Lake Worth. Whether it’s a wedding, prom, or event, our buses offer premium features, elegant interiors, and competitive pricing. Book your Lake Worth party bus today and enjoy a seamless, unforgettable experience with our professional team.

  2. Explore Miami’s vibrant party bus scene with options for affordable rentals. Visit bestmiamipartybus.com for rental inquiries and services. Find the perfect party bus rental in Miami, FL, to suit your budget and event needs, ensuring a memorable celebration experience.

  3. Party Bus Miami brings the fun wherever you go! Whether it’s a vibrant birthday party bus Miami, a magical prom party bus Miami, or an exciting kids party bus Miami, we make it memorable. Dance on our party bus with pole Miami or relax with views from a Miami Beach party bus.

  4. Treat your guests to a fun-packed Birthday Party Bus Miami! It’s the coolest way to celebrate with music, lights, and non-stop entertainment. Our Kids & Adult Birthday Party Bus Miami is safe, spacious, and stylish. Make your day special with a Luxury Birthday Party Bus Miami and a top-rated Birthday Party Bus Rental Miami.

  5. Explore wheelchair-accessible car services and group transportation alternatives in Washington DC. With a plethora of options, including shuttles and general transportation, the capital ensures seamless travel experiences. Consider KVLIMO for top-tier transportation solutions that prioritize comfort and reliability.

  6. Count on Nassau Airport Shuttle Transfer for flexible Nassau airport shuttle service, ideal for group airport shuttle Nassau Bahamas or private Nassau airport transportation. Enjoy a worry-free shuttle transfer from Nassau airport to your destination.

  7. Ride in comfort with Luxury Chauffeur Services Dubai, ideal for business travelers and tourists. Our Chauffeur Driven Car Dubai service ensures you get where you need to be with class. Book a VIP Chauffeur Dubai today for private, professional, and executive-level travel throughout the city.

  8. Book a Pickup from Dubai International Airport for a stress-free start to your trip. We offer Private Airport Transfer Dubai, Dubai Airport to Hotel Shuttle, and Dubai Airport Executive Transfer for business and leisure travelers. Choose a Dubai Airport Limousine Transfer for an elegant ride.

  9. Discover Dubai in style with our luxurious stretch limousines. Experience opulent travel, whether for special occasions, city tours, or airport transfers. Elevate your Dubai experience with our premium stretch limousine service.

  10. Black Tie Transport offers reliable shuttle service from Rancho Cucamonga to LAX, featuring affordable LAX shuttle options, private car services, and luxury transportation. With airport shuttle options, SUV transportation, and seamless Rancho Cucamonga airport transfers, choose Black Tie for a comfortable and efficient airport experience to LAX and beyond.

Leave a Reply

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