Object-Oriented Programming (OOP) in
Java – Detailed Guide
1. Class and Object
A class is a blueprint for creating objects.
An object is an instance of a class, with its own values for fields (attributes) and access to
methods (behaviors).
Example Code:
class Car {
String brand;
int speed;
void drive() {
System.out.println(brand + " is driving at " + speed + " km/h.");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.speed = 120;
myCar.drive();
}
}
2. Encapsulation
Encapsulation means wrapping data and methods into a single unit (class).
It hides internal state using private access and exposes behavior using public methods
(getters and setters).
Example Code:
class Employee {
private int salary;
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
if (salary > 0)
this.salary = salary;
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setSalary(50000);
System.out.println(emp.getSalary());
}
}
3. Inheritance
Inheritance allows a class (subclass) to inherit fields and methods from another class
(superclass).
Example Code:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
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.eat();
dog.bark();
}
}
4. Polymorphism
Polymorphism allows the same method to behave differently based on the object or input
type.
a) Compile-time Polymorphism (Method Overloading):
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.add(10, 20));
System.out.println(c.add(10.5, 5.5));
}
}
b) Runtime Polymorphism (Method Overriding):
class Animal {
void sound() {
System.out.println("Some sound...");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Cat();
a.sound();
}
}
5. Abstraction
Abstraction hides internal details and shows only essential information.
a) Abstract Class:
abstract class Shape {
abstract void draw();
void message() {
System.out.println("This is a shape.");
}
}
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();
s.message();
}
}
b) Interface:
interface Vehicle {
void start();
}
class Bike implements Vehicle {
public void start() {
System.out.println("Bike started.");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Bike();
v.start();
}
}
Summary Table
| Concept | Purpose | Keyword(s) |
|----------------|-----------------------------------------|----------------------|
| Class & Object | Blueprint and instances | class, new |
| Encapsulation | Data hiding | private, get/set |
| Inheritance | Code reuse | extends |
| Polymorphism | Many forms (overloading/overriding) | @Override, overload |
| Abstraction | Show only essential info | abstract, interface |