Object-Oriented Programming (OOP) is a programming approach based on classes and objects. Java uses OOP concepts to organize code, represent real-world entities, and build reusable and maintainable applications.
OOP stands for Object-Oriented Programming. It organizes programs around objects that combine data and behavior.
For example, a Student object can contain:
class Student {
String name = "Rahul";
int age = 20;
void display() {
System.out.println(name);
System.out.println(age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Rahul
20
The class defines the structure, and the object stores its own instance data and uses its methods.
The four commonly taught pillars of OOP are:
| Concept | Meaning |
|---|---|
| Encapsulation | Combining data and methods while controlling access to data. |
| Inheritance | Creating a class based on another class. |
| Polymorphism | Allowing the same operation or interface to have different behaviors. |
| Abstraction | Exposing essential features while hiding implementation details. |
A class is a blueprint. An object is an instance created from that class.
class Car {
String brand;
String color;
void display() {
System.out.println(brand + " " + color);
}
public static void main(String[] args) {
Car c1 = new Car();
Car c2 = new Car();
c1.brand = "Toyota";
c1.color = "White";
c2.brand = "Honda";
c2.color = "Black";
c1.display();
c2.display();
}
}
Toyota White
Honda Black
Both objects belong to the same class but contain different instance field values.
Encapsulation combines data and methods within a class. A common approach is to make fields private and provide public methods to access or update them safely.
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(1000);
System.out.println(account.getBalance());
}
}
1000.0
The private field cannot be accessed directly from outside the class. The public methods control how its value is used.
Inheritance allows a subclass to extend a superclass and reuse its accessible members.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
public static void main(String[] args) {
Dog d1 = new Dog();
d1.eat();
d1.bark();
}
}
Animal is eating
Dog is barking
The Dog class extends Animal,
so a Dog object can use the inherited
eat() method.
Polymorphism means "many forms." In Java, a method call can produce different behavior depending on the applicable method overload or the runtime object.
Example: Runtime polymorphism
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
class Main {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound();
a2.sound();
}
}
Dog barks
Cat meows
The method implementation is selected according to the actual object at runtime.
Abstraction focuses on what an object does rather than exposing all the details of how it works. Java supports abstraction through abstract classes and interfaces.
abstract class Shape {
abstract double calculateArea();
}
class Rectangle extends Shape {
double length;
double width;
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
double calculateArea() {
return length * width;
}
}
class Main {
public static void main(String[] args) {
Shape s1 = new Rectangle(10, 5);
System.out.println(s1.calculateArea());
}
}
50.0
The Shape abstraction defines an operation, while Rectangle provides the implementation.
Constructors initialize objects when they are created. They can assign initial values to an object's fields.
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Rahul", 20);
s1.display();
}
}
Rahul 20
The constructor receives values and initializes the new Student object.
Method overloading allows multiple methods in the same class to have the same name but different parameter lists.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator c1 = new Calculator();
System.out.println(c1.add(10, 20));
System.out.println(c1.add(10, 20, 30));
}
}
30
60
The compiler selects the appropriate overloaded method based on the arguments.
Method overriding occurs when a subclass provides its own implementation of an inherited instance method.
class Parent {
void display() {
System.out.println("Parent class");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child class");
}
public static void main(String[] args) {
Parent p1 = new Child();
p1.display();
}
}
Child class
The @Override annotation helps the
compiler verify that the method correctly overrides
an inherited method.
class Employee {
private String name;
private double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
class Manager extends Employee {
private String department;
Manager(String name, double salary, String department) {
super(name, salary);
this.department = department;
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Department: " + department);
}
}
class Main {
public static void main(String[] args) {
Manager m1 = new Manager(
"Amit", 50000, "IT"
);
m1.displayDetails();
}
}
Name: Amit
Salary: 50000.0
Department: IT
This example demonstrates encapsulation through
private fields, inheritance through
extends, constructor chaining through
super(), and method overriding.
start().
add() methods
that accept different numbers of integers.
1. What does OOP stand for?
2. Which OOP concept combines data and methods and can control access to data?
3. Which keyword is used to inherit a class in Java?