22 Oct 2023




Beginner

Object-Oriented Programming (OOP) is a way of organizing and structuring code based on the concept of objects, which represent real-world things and their interactions in a software system. OOP uses classes to define these objects, and it emphasizes principles like abstraction, encapsulation, inheritance, and polymorphism for creating modular and reusable code.

  • Object: An object represents a specific entity in the real world, such as a person, car, or bank account. It combines data (attributes) and functionality (methods) related to that entity.
  • Class: A class is a blueprint for creating objects. It defines the attributes and methods that all objects of that class will share.
  • Inheritance: Inheritance allows new classes to be defined by inheriting attributes and methods from existing classes. This promotes code reuse and reduces redundancy.
  • Encapsulation: Encapsulation restricts direct access to an object's internal data. Instead, data is accessed and modified through methods, ensuring data integrity and consistency.
  • Polymorphism: Polymorphism allows objects to exhibit different behaviors in response to the same method or function call, depending on their specific type or class. This enables flexible and dynamic behavior in programs.
  • Abstraction: Abstraction focuses on essential features of an object while hiding unnecessary details. This simplifies code and promotes reusability.

Class and Object:

  Class (Blueprint)
    |
    +-------------------+
    |   Attributes     |
    |   (Data/State)   |
    +-------------------+
    |   Methods        |
    |   (Functions)    |
    +-------------------+
    
  Object (Instance)
    |
    +-------------------+
    |   Attributes     |
    |   (Data/State)   |
    +-------------------+
    |   Methods        |
    |   (Functions)    |
    +-------------------+

In the diagram, a class defines the structure of an object. It has attributes (data or state) and methods (functions). When you create an object from a class, it inherits these attributes and methods, and you can modify and use them according to your needs.

Examples:

  1. Class: Car

    • Attributes: Make, Model, Year
    • Methods: Start(), Stop(), Accelerate()
  2. Class: BankAccount

    • Attributes: Account Number, Balance
    • Methods: Deposit(), Withdraw(), CheckBalance()
  3. Class: Dog

    • Attributes: Name, Breed, Age
    • Methods: Bark(), Eat(), Sleep()

In the first example, a "Car" class represents the blueprint for creating car objects. It defines attributes like make, model, and year, and methods like start, stop, and accelerate. You can create instances of this class, each representing a specific car.

In the second example, a "BankAccount" class represents the blueprint for bank account objects. It includes attributes such as account number and balance, along with methods for depositing, withdrawing, and checking the balance.

In the third example, a "Dog" class defines attributes like name, breed, and age, along with methods for actions dogs can perform, like barking, eating, and sleeping.

object-oriented-programming
classes-objects
oop