Hackforge Academy

Category: java

What is oops in java

Published on 16 Jun 2026

Explanation

OOPS (Object-Oriented Programming System) in Java is a programming paradigm based on objects and classes. The four main principles are Encapsulation, Inheritance, Polymorphism, and Abstraction.

Code:

class Car {
    String brand;

    Car(String brand) {
        this.brand = brand;
    }

    void display() {
        System.out.println("Brand: " +
 brand);
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car("Toyota");
        car.display();
    }
}

Explanation

Encapsulation is the process of wrapping data and methods into a single unit (class) and restricting direct access to data using access modifiers.

Code:

class Employee {
    private String name;

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

    public String getName() {
        return name;
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.setName("John");
        System.out.println(emp.getName());
    }
}

Explanation

Inheritance allows one class to acquire the properties and behaviors of another class, promoting code reusability.

Code:

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

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

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

Explanation

Polymorphism allows the same method or interface to behave differently based on the object. Method overriding is a common example.

Code:

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

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

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.sound();
    }
}

Explanation

Abstraction hides implementation details and exposes only essential functionality. It can be achieved using abstract classes or interfaces.

Code:

abstract class Shape {
    abstract void draw();
}

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

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

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community