1-Introduction to OOPs in Java (Object-Oriented Programming)

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 name
  • rollNo, name โ†’ instance variables
  • display() โ†’ 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 ๐Ÿง 

  1. Allocates memory in heap
  2. Calls the constructor
  3. 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 variable
  • new Student() โ†’ object created in heap
  • s1.rollNo = 101 โ†’ assigning data to object
  • s1.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) calls fact(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

ModifierSame ClassSame PackageSubclassEverywhere
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

  • this vs 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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *