What is OOPS in Java?

First, OOPS stands for Object-Oriented Programming Structure, a feature that uses “objects” to design software. Java, being an object-oriented language, follows the principles of OOP to create modular, reusable, and maintainable code.

oops in java

Class is a blueprint or template of objects that don’t have any physical existence and don’t take any memory. It defines a set of attributes and methods that the objects instantiated from the class will possess.

An object is an instance of a class, which has a physical existence and takes some space in memory.

Example:
class Car {
    String color;
    String model;
    Integer wheels;
    
    void displayDetails() {
        System.out.println("Model: " + model + ", Color: " + color + "Wheels: " + wheels);
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();// myCar is the object of Car class
        myCar.color = "Black";
        myCar.model = "Audi Model Q4";
        myCar.wheels = 4;
        myCar.displayDetails();  // Output: Model: Audi Model Q4, Color: Black, wheels: 4
    }
}
The Problem Solution

Inheritance is a mechanism that promotes code reusability, where one class acquires the properties and methods of another class. The class that inherits the properties is called the subclass, and the class from which properties are inherited is called the superclass.

Example:
class Vehicle {
    String brand;
    
    void blowHorn() {
        System.out.println("Beep!! Beep!! Beep!!");
    }
}

class Car extends Vehicle {
    String model;
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.brand = "Audi";
        myCar.model = "Model Q4";
        myCar.blowHorn();  // Output: Beep!! Beep!! Beep!!
        System.out.println(myCar.brand + " " + myCar.model);  // Output: Audi Model Q4
    }
}
The Problem Solution

polymorphism in Java is the ability of a single method or object to take many forms. This can be achieved through overriding and overloading methods.

Method overloading:

It occurs when multiple methods in the same class have the same name but different parameters.

Example:
class MethodOverloading{
    int sum(int x, int y) {
        return x + y;
    }
    
    double sum(double x, double y) {
        return x + y;
    }
}
The Problem Solution
Method Overriding:

It occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.

Example:
class Vechil {
    void wheel() {
        System.out.println("Vechile has wheels");
    }
}

class Audi extends Vechil {
    @Override
    void wheel() {
        System.out.println("Audi has 4 wheels");
    }
}

class Activa extends Vechil {
    @Override
    void wheel() {
        System.out.println("Activa has 2 wheels");
    }
}

public class Main {
    public static void main(String[] args) {
        Vechil myCar = new Audi();
        Vechil myActiva = new Activa();
        myCar.wheel();  // Output: Audi has 4 wheels
        myActiva.wheel();  // Output: Activa has 2 wheels
    }
}
The Problem Solution

Abstraction in OOPS

Abstraction simplifies complexity by hiding intricate implementation details and emphasizing essential features of an object. In Java, this is realized through abstract classes and interfaces, enabling a clearer and more modular design of software systems.

Example:
abstract class Vachile {
    abstract void sound();
    
    void brands() {
        System.out.println("This Vachile has different brands");
    }
}

class Car extends Vachile {
    @Override
    void brands() {
        System.out.println("Audi, BMW, VOLVO");
    }
}
The Problem Solution

Encapsulation in OOPS

Encapsulation is the technique of wrapping the data member (variables) and the member function(methods) together as a single unit. It restricts direct access to some of an object’s components, which can protect the integrity of the data.

Example:
class Person {
    private String name;
    private String profession;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getProfession() {
        return profession;
    }
    
    public void setProfession(String profession) {
        this.profession = profession;
    }
}

public class Main {
    public static void main(String[] args) {
        Person person = new Person();
        person.setName("Virat Kohli");
        person.setProfession("Cricketer");
        System.out.println(person.getName() + " is a " + person.getProfession);
        // Output: Virat Kohli is a Cricketer
    }
}
The Problem Solution

Pros of OOPS in Java

  1. Easy to Manage: OOPS helps break down a big program into smaller parts, making it easier to work on and understand.
  2. Reusability: Once you create a class, you can use it in different programs without writing the code again.
  3. Data Protection: OOPS hides the data inside classes, only exposing what is necessary, which keeps the data safe and secure.
  4. Easier to Extend: You can add new features without changing existing code, making it simpler to update.
  5. Flexible Code: OOPS lets methods perform different tasks based on the object they work on, making the code adaptable.

Cons of OOPS in Java

  1. More Memory Use: Objects take up more memory, which can be a problem in limited memory environments.
  2. Complex Design: Designing an OOP system requires careful planning and can take more time, especially for small projects.
  3. Overhead in Development: It can take more time to design and develop an OOP-based system compared to simpler approaches.

Leave a Reply

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