OOPS REVISION - 1 | JAVA

What the hell is OOP ??!!

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data and code. Data in objects are stored in fields (also known as attributes or properties), and code is in the form of procedures (often known as methods). OOP focuses on using objects to design and build applications. This approach promotes greater flexibility and maintainability in programming by structuring software around objects rather than actions and logic.

What is OOP made up of ?!

The core building blocks of OOP are:

  1. Objects: Instances of classes that represent entities with state and behavior.
  2. Classes: Blueprints for creating objects. They encapsulate data for the object and methods to manipulate that data.
  3. Methods: Functions defined inside a class that describe the behaviors of the objects.
  4. Attributes: Variables defined inside a class that hold the data for the objects.

Real World Example:

1. Objects
Analogy: A Specific Car

Think of a specific car you own, like a red Tesla Model S. This car is an object. It has specific attributes such as its color (red), make (Tesla), and model (Model S). It also has behaviors, like the ability to start, stop, and accelerate.
In OOP, an object is a specific instance of a class, containing real values for its properties and the ability to perform actions defined by its methods.

2. Classes
Analogy: Car Blueprint

Consider the blueprint or template for a car. This blueprint defines the general attributes and behaviors any car should have, like wheels, engine, color, and the ability to drive, stop, and honk.
In Oop, a class is like this blueprint. It provides the structure and behavior that objects created from the class will have, but it doesn't represent any one specific car by itself.

3. Methods
Analogy: Car Functions

A car has various functions like starting the engine, honking the horn, or accelerating. Each of these functions describes a specific behavior that the car can perform.
In Oop, methods are the functions defined within a class that describe the behaviors of the objects. For example, the startEngine() method would define the actions that occur when a car’s engine is started.

4. Attributes
Analogy: Car Specifications

Each car has specific details or specifications, such as its color, make, model, and engine size. These details are what make each car unique.
In Oop, attributes (also known as properties or fields) are the variables that hold data specific to an object. For a car object, attributes could include color, make, and model.

Code for better understanding:

// Define the Car class
public class Car {
    // Attributes of the Car class
    String color;
    String make;
    String model;
    
    // Method to start the car
    void start() {
        System.out.println("Car is starting");
    }
    
    // Method to stop the car
    void stop() {
        System.out.println("Car is stopping");
    }
    
    // Method to accelerate the car
    void accelerate() {
        System.out.println("Car is accelerating");
    }
}

// Main class to create and interact with Car objects
public class Main {
    public static void main(String[] args) {
        // Creating an object of the Car class
        Car myCar = new Car();
        myCar.color = "Red";
        myCar.make = "Tesla";
        myCar.model = "Model S";
        
        // Using the methods of the Car object
        myCar.start();
        myCar.accelerate();
        myCar.stop();
        
        // Displaying the details of the Car object
        System.out.println("Car: " + myCar.make + " " + myCar.model + ", Color: " + myCar.color);
    }
}

In this example:

  • The class Car serves as the blueprint.
  • The object myCar is a specific instance of the Car class.
  • The attributes color, make, and model hold the details of this specific car.
  • The methods start(), stop(), and accelerate() define the behaviors of the car.

Principles/Pillars of OOP:

There are majorly 4 principles on which OOP is based on:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

To be continued in next post....

Link to the second article : https://leetcode.com/discuss/interview-question/object-oriented-design/5321424/OOPS-REVISION-2-or-JAVA

Comments (3)