OOPS Concepts in Java

7/24/2025

Diagram explaining OOPS concepts in Java - Abstraction, Inheritance, Polymorphism, Encapsulation

Go Back

Understanding OOPS Concepts in Java: A Complete Guide for Beginners

Abstraction | Encapsulation | Inheritance | Polymorphism

Author: Shubham mishra
Published: 24th July 2025
Tags: Java, OOPS, Object-Oriented Programming, Abstraction, Encapsulation


Why Learn OOPS in Java?

Object-Oriented Programming (OOP) is the foundation of Java. These concepts help in designing flexible and modular software that is easier to maintain and scale. Java follows the OOP paradigm which focuses on using objects—instances of classes—to perform actions. OOP enhances code reusability, security, and extensibility.

With Java’s consistent presence in developer surveys and its usage across enterprise systems, mobile applications, and backend services, mastering OOP is crucial for any Java developer.


 Diagram explaining OOPS concepts in Java - Abstraction, Inheritance, Polymorphism, Encapsulation

Key OOPS Concepts in Java

1. Objects and Classes

A class in Java is a blueprint or prototype from which objects are created. It defines the properties (attributes) and behaviors (methods) of an object.

An object is an instance of a class that can access its data and methods.

class Car {
  String brand = "Tesla";
  void drive() {
    System.out.println("Driving the car...");
  }
}

public class Main {
  public static void main(String[] args) {
    Car myCar = new Car();
    System.out.println(myCar.brand);
    myCar.drive();
  }
}

Output:
Tesla
Driving the car...


2. Abstraction in Java

Abstraction is the process of hiding complex internal details and showing only the necessary features of an object.

abstract class Vehicle {
  abstract void startEngine();
  
  void fuelType() {
    System.out.println("Diesel or Petrol");
  }
}

class Truck extends Vehicle {
  void startEngine() {
    System.out.println("Truck engine started");
  }
}

public class AbstractionDemo {
  public static void main(String[] args) {
    Vehicle v = new Truck();
    v.startEngine();
    v.fuelType();
  }
}

Output:
Truck engine started
Diesel or Petrol


3. Encapsulation in Java

Encapsulation is the mechanism of wrapping data (variables) and code (methods) into a single unit. It restricts direct access to some of the object's components.

class Employee {
  private String name;
  private int age;

  public void setName(String name) {
    this.name = name;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }
}

public class EncapsulationDemo {
  public static void main(String[] args) {
    Employee emp = new Employee();
    emp.setName("Rahul");
    emp.setAge(30);

    System.out.println("Name: " + emp.getName());
    System.out.println("Age: " + emp.getAge());
  }
}

Output:
Name: Rahul
Age: 30


4. Inheritance in Java

Inheritance allows one class (subclass/child) to inherit fields and methods from another class (superclass/parent). Java supports Single, Multilevel, and Hierarchical inheritance.

class Animal {
  void sound() {
    System.out.println("Animal makes a sound");
  }
}

class Dog extends Animal {
  void bark() {
    System.out.println("Dog barks");
  }
}

public class InheritanceDemo {
  public static void main(String[] args) {
    Dog d = new Dog();
    d.sound();
    d.bark();
  }
}

Output:
Animal makes a sound
Dog barks


5. Polymorphism in Java

Polymorphism lets us perform the same action in different ways. It is categorized into:

  • Compile-time Polymorphism (Method Overloading)

  • Runtime Polymorphism (Method Overriding)

Method Overloading (Static Polymorphism)

class Calculator {
  int add(int a, int b) {
    return a + b;
  }

  double add(double a, double b) {
    return a + b;
  }
}

public class OverloadingDemo {
  public static void main(String[] args) {
    Calculator c = new Calculator();
    System.out.println(c.add(5, 6));
    System.out.println(c.add(3.5, 2.1));
  }
}

Output:
11
5.6

Method Overriding (Dynamic Polymorphism)

class Shape {
  void draw() {
    System.out.println("Drawing Shape");
  }
}

class Circle extends Shape {
  void draw() {
    System.out.println("Drawing Circle");
  }
}

public class OverridingDemo {
  public static void main(String[] args) {
    Shape s = new Circle();
    s.draw();
  }
}

Output:
Drawing Circle


Applications of OOPS in Java

  1. Enterprise Applications – Java’s OOP principles power frameworks like Spring and Hibernate.

  2. Android Development – Android apps are built using Java classes and objects.

  3. Game Development – Entities like player, enemy, and score are modeled as objects.

  4. Banking Systems – Account, Transaction, and Customer are encapsulated in secure object models.


Final Thoughts

OOPs is not just a concept—it’s the backbone of Java programming. Understanding Abstraction, Encapsulation, Inheritance, and Polymorphism is critical to becoming an efficient Java developer. Whether you're building enterprise software or simple Java applications, these principles guide better code organization and long-term scalability.


About Developer Indian
We are a platform focused on Java, backend engineering, and developer tools. Want to master core Java and backend concepts? Explore our Medium blog and stay updated with insightful tutorials and guides.

Table of content