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();
}
}