What is OOPs in Java?
Object-Oriented Programming (OOP) in Java is a programming paradigm that focuses on using objects and classes to organize code. It helps developers create modular, reusable, and maintainable software systems by modeling real-world entities.
Core OOPs Concepts in Java
1. Class
A class is a blueprint or template for creating objects. It defines the data (fields) and behavior (methods) that the objects will have.
Example:
class Car {
String color;
void drive() {
System.out.println("Car is driving");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();
}
}
2. Object
An object is an instance of a class. It represents real-world entities like a car, employee, or student. Each object has its own data and can perform actions defined by its class.
Example:
Car car1 = new Car();
Car car2 = new Car();
car1.color = "Blue";
car2.color = "Black";
car1.drive();
car2.drive();
3. Inheritance
Inheritance allows one class (child) to acquire the properties and methods of another class (parent). It promotes code reusability and hierarchical relationships.
Example:
class Vehicle {
void start() {
System.out.println("Vehicle started");
}
}
class Bike extends Vehicle {
void wheelie() {
System.out.println("Bike doing a wheelie");
}
}
public class Main {
public static void main(String[] args) {
Bike b = new Bike();
b.start();
b.wheelie();
}
}
4. Polymorphism
Polymorphism means "many forms." It allows a single function, class, or interface to behave differently based on the context — primarily through method overloading and overriding.
Example:
class Animal {
void sound() { System.out.println("Animal makes a sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting
a.sound(); // Output: Dog barks
}
}
5. Encapsulation
Encapsulation is the process of wrapping data and methods into a single unit (class) and restricting direct access using private fields and public getters/setters.
Example:
class Student {
private String name;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.setName("Amit");
System.out.println(s.getName());
}
}
6. Abstraction
Abstraction hides implementation details and exposes only necessary functionalities. It can be achieved using abstract classes or interfaces in Java.
Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
Best Practices for OOP in Java
Use classes and objects to model real-world problems accurately
Keep your data encapsulated and maintain data integrity
Reuse code using inheritance and polymorphism effectively
Abstract implementation details to build clean and flexible APIs