BCA / B.Tech 15 min read

OOPs Concepts in Java

Concepts of OOPS – What are the Concepts of OOPS?

What is OOPs (Object-Oriented Programming)?

  • OOPs (Object-Oriented Programming) is a programming style where data and the processes that act on it are bundled together.
  • Its purpose is to organize code, make it reusable, and increase security.
Real-life Example:

Imagine a Car is an object. It has properties like color, model, and speed, and methods (actions) like start, brake, and accelerate.

Similarly, in Java, we create objects while programming. OOPs is based on this concept.

The 4 Main Principles of OOPs:

There are four main principles of OOPs in Java:

Abstraction (Hiding complexity)
Encapsulation (Bundling data and methods)
Inheritance (Getting properties from another class)
Polymorphism (One thing, many forms)

Abstraction (Hiding Complexity)

Definition: Abstraction means showing essential information and hiding the rest.

Real-life Example:

  • When you drive a car, you only use the steering wheel, brakes, and accelerator.
  • You don’t see what’s happening inside the engine.
In Java:

Abstraction is implemented using abstract classes and interfaces.

Example: Abstraction


// Abstract Class
abstract class Vehicle {
    abstract void start(); // Only the method name (no body)
    
    void stop() { // Normal method
        System.out.println("The vehicle has stopped.");
    }
}

// Subclass
class Car extends Vehicle {
    void start() { // It is necessary to define the abstract method
        System.out.println("The car has started.");
    }
}

public class Main {
    public static void main(String args[]) {
        Vehicle myCar = new Car();
        myCar.start(); // Output: The car has started.
        myCar.stop();  // Output: The vehicle has stopped.
    }
}
        
Here, the start() method was hidden, and the Car class implemented it.

Encapsulation (Bundling Data)

Definition: Encapsulation means bundling data and its related methods together to keep it safe from the outside world.

Real-life Example: Your mobile phone – all its code is inside, but you just press buttons to use it.

In Java:
  • Data is secured by making it private.
  • That data is accessed through getter and setter methods.
Example: Encapsulation

class Person {
    private String name; // Private variable (cannot be accessed directly from outside)

    // Setter method (to set the name)
    public void setName(String newName) {
        this.name = newName;
    }

    // Getter method (to get the name)
    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String args[]) {
        Person p = new Person();
        p.setName("Ajay");   // Set the name
        System.out.println(p.getName()); // Output: Ajay
    }
}
        
Here, the name is made private to prevent direct access, and getter-setter methods are used.

Inheritance (Getting Properties)

Definition: Inheritance means one class taking properties and methods from another class.

Real-life Example: A son inherits some traits from his father (like eye color, height, behavior, etc.).

In Java:

Inheritance is done using the `extends` keyword.

Example: Inheritance

// Parent class
class Animal {
    void eat() {
        System.out.println("The animal is eating.");
    }
}

// Child class (taking properties from Animal)
class Dog extends Animal {
    void bark() {
        System.out.println("The dog is barking.");
    }
}

public class Main {
    public static void main(String args[]) {
        Dog myDog = new Dog();
        myDog.eat();  // Output: The animal is eating.
        myDog.bark(); // Output: The dog is barking.
    }
}
        
Here, the Dog class used the eat() method of the Animal class.

Polymorphism (One Thing, Many Forms)

Definition: Polymorphism means having different forms for the same thing.

Real-life Example: A single button on your mobile phone does different things – a short press might end a call, while a long press might lock the screen.

In Java:
  • Method Overloading (methods with the same name but different parameters).
  • Method Overriding (redefining a parent class's method in a child class).

Example: Method Overloading

class MathOperation {
    int add(int a, int b) {
        return a + b;
    }
    
    int add(int a, int b, int c) { // Same method name, different parameters
        return a + b + c;
    }
}

public class Main {
    public static void main(String args[]) {
        MathOperation obj = new MathOperation();
        System.out.println(obj.add(5, 10));     // Output: 15
        System.out.println(obj.add(5, 10, 20)); // Output: 35
    }
}
        
Here, there are two versions of the add() method (Method Overloading).

Advantages of OOPs:

Code Reusability: No need to write code again and again due to inheritance.
Security: Data remains secure through encapsulation.
Flexibility: Polymorphism allows the same method to work in different situations.
Better Organization: Code can be organized properly.