Java is a class-based, object-oriented programming language designed to build structured, reusable, and maintainable software โ
While basic Java focuses on syntax and control flow, Object-Oriented Programming (OOPs) focuses on design, structure, and real-world modeling.
OOPs organizes a program around objects, which combine data and behavior into a single unit ๐ฆ
This approach makes applications easier to understand, extend, and maintain over time.
This article provides a detailed introduction to OOPs in Java, explaining each concept with complete runnable code, step-by-step explanations, and common pitfalls.
๐ง What Is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm where software is built using objects that represent real-world entities ๐
Each object:
- Stores data (variables)
- Performs actions (methods)
By grouping related data and behavior together, OOP improves:
- Code readability ๐
- Reusability โป๏ธ
- Maintainability ๐ง
๐งฑ Classes, Objects, and Methods
These are the foundation of OOPs in Java.
๐๏ธ Class
A class is a blueprint that defines:
- What data an object will store
- What actions an object can perform
A class does not execute and does not occupy memory by itself.
โ๏ธ Example: Defining a Class
class Student {
int rollNo;
String name;
void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
}
}
๐ง Explanation
Studentโ class namerollNo,nameโ instance variablesdisplay()โ instance method- No memory is allocated yet
๐ฏ Object and new Operator
An object is a real instance of a class.
Objects are created using the new operator.
What new Does Internally ๐ง
- Allocates memory in heap
- Calls the constructor
- Returns reference to the object
โถ๏ธ Complete Runnable Program (Class + Object + Method)
class Student {
int rollNo;
String name;
void display() {
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.rollNo = 101;
s1.name = "Ajay";
s1.display();
}
}
๐ง Line-by-Line Explanation
Student s1โ reference variablenew Student()โ object created in heaps1.rollNo = 101โ assigning data to objects1.display()โ method call using object reference
๐ง Constructor
A constructor is a special method that initializes an object.
๐ Key Rules
- Same name as class
- No return type
- Automatically called during object creation
โถ๏ธ Constructor Example (Complete Code)
class Student {
int rollNo;
String name;
Student(int r, String n) {
rollNo = r;
name = n;
}
void display() {
System.out.println(rollNo + " " + name);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
Student s1 = new Student(101, "Ajay");
s1.display();
}
}
๐ง How This Works
new Student(101, "Ajay")calls the constructor- Values are assigned during object creation
- Constructor runs only once per object
๐ Constructor Overloading
When a class has multiple constructors with different parameter lists, it is called constructor overloading.
class Box {
int length, width;
Box() {
length = 0;
width = 0;
}
Box(int l, int w) {
length = l;
width = w;
}
}
public class ConstructorOverloadDemo {
public static void main(String[] args) {
Box b1 = new Box();
Box b2 = new Box(10, 20);
}
}
๐ง Explanation
- JVM selects constructor based on arguments
- Enables flexible object creation
๐งฎ Method Overloading
Method overloading allows multiple methods with the same name but different parameters.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class MethodOverloadDemo {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.add(10, 20));
System.out.println(c.add(10, 20, 30));
}
}
๐ง Explanation
- Method selection happens at compile time
- Return type alone cannot overload methods โ
๐ Recursion
A method calling itself is called recursion ๐
class Factorial {
int fact(int n) {
if (n == 1)
return 1;
return n * fact(n - 1);
}
}
public class RecursionDemo {
public static void main(String[] args) {
Factorial f = new Factorial();
System.out.println(f.fact(5));
}
}
๐ง Explanation
fact(5)callsfact(4)โfact(3)โ …- Base condition stops recursion
- Missing base condition causes StackOverflowError โ
๐ Passing and Returning Objects from Methods
Objects can be passed and returned like variables.
class Test {
int value;
Test(int v) {
value = v;
}
Test increment(Test t) {
return new Test(t.value + 1);
}
}
public class ObjectPassingDemo {
public static void main(String[] args) {
Test t1 = new Test(10);
Test t2 = t1.increment(t1);
System.out.println(t2.value);
}
}
๐ง Explanation
- Object reference is passed
- New object is returned
- Original object remains unchanged
๐ this Keyword
this refers to the current object.
class Demo {
int x;
Demo(int x) {
this.x = x;
}
}
public class ThisDemo {
public static void main(String[] args) {
Demo d = new Demo(10);
}
}
๐ง Why this?
- Differentiates instance variable from parameter
- Refers to current object in memory
๐ static Keyword
static members belong to the class, not to objects.
static Variable
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println(count);
}
}
public class StaticVarDemo {
public static void main(String[] args) {
new Counter();
new Counter();
}
}
๐ง Explanation
- One copy shared across all objects
- Memory allocated once
static Method
class Utility {
static void show() {
System.out.println("Static method");
}
}
public class StaticMethodDemo {
public static void main(String[] args) {
Utility.show();
}
}
๐ Access Control & Modifiers
Java controls visibility using access modifiers.
๐ Access Modifier Table
| Modifier | Same Class | Same Package | Subclass | Everywhere |
|---|---|---|---|---|
| private | โ | โ | โ | โ |
| default | โ | โ | โ | โ |
| protected | โ | โ | โ | โ |
| public | โ | โ | โ | โ |
class AccessDemo {
private int a = 10;
public int b = 20;
}
๐งฌ Nested and Inner Classes
Static Nested Class
class Outer {
static class Inner {
void show() {
System.out.println("Static nested class");
}
}
}
public class NestedDemo {
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.show();
}
}
Inner Class (Non-static)
class Outer {
class Inner {
void show() {
System.out.println("Inner class");
}
}
}
public class InnerDemo {
public static void main(String[] args) {
Outer o = new Outer();
Outer.Inner i = o.new Inner();
i.show();
}
}
๐ญ Abstract Class
An abstract class cannot be instantiated.
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
}
public class AbstractDemo {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
๐ Java Standard Libraries
Java provides built-in libraries for common tasks.
import java.util.ArrayList;
public class LibraryDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("OOPs");
System.out.println(list);
}
}
โ Common Mistakes and Confusions
โ Common Mistakes
- Writing everything inside
main() - Misusing
static - Ignoring encapsulation
- Confusing class and object
โ Common Confusions
thisvs reference variable- Overloading vs overriding
- static vs instance members
โ Conclusion
Object-Oriented Programming forms the core of Java development โ
By mastering:
- Classes, objects, and methods
- Constructors and overloading
- Recursion and object passing
this,static, access control- Nested classes and abstraction
- Java standard libraries
Java programs become clean, structured, reusable, and scalable ๐
OOPs is not just a concept โ it is the foundation of professional Java programming.