⬅ Previous Next ➡

Object-Oriented Programming (OOP)

Core OOP in Java: Class, Object, Constructor, Methods, Access Modifiers, Packages, Encapsulation, Abstraction, static & this
  • OOP (Object-Oriented Programming) organizes code using classes and objects.
  • Main benefits: reusability, security, easy maintenance, and real-world modeling.

1) Classes and Objects

  • Class is a blueprint (defines data + behavior).
  • Object is an instance of a class (real usable entity).
class Student {
    String name;
    int marks;

    void show() {
        System.out.println("Name: " + name + ", Marks: " + marks);
    }

    public static void main(String[] args) {
        Student s1 = new Student(); // object
        s1.name = "Sourav";
        s1.marks = 82;
        s1.show();
    }
}

2) Constructors

  • Constructor is a special method used to initialize objects.
  • Same name as class, no return type.
  • Types: default and parameterized.
class User {
    String email;
    int age;

    // Default constructor
    User() {
        email = "not-set";
        age = 0;
    }

    // Parameterized constructor
    User(String email, int age) {
        this.email = email; // this refers to current object
        this.age = age;
    }

    void printUser() {
        System.out.println("Email: " + email + ", Age: " + age);
    }

    public static void main(String[] args) {
        User u1 = new User();
        User u2 = new User("souravshu562@gmail.com", 25);
        u1.printUser();
        u2.printUser();
    }
}

3) Methods

  • Method is a function inside a class used to perform tasks.
  • Can take parameters and return values.
class Calc {
    int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        Calc c = new Calc();
        int ans = c.add(10, 20);
        System.out.println("Sum: " + ans);
    }
}

4) Access Modifiers

  • Control visibility (who can access class/members).
  • public: accessible everywhere
  • private: accessible only inside same class
  • protected: same package + subclass
  • default (no keyword): same package only
class AccessDemo {
    public int pub = 10;
    protected int pro = 20;
    int def = 30;         // default
    private int pri = 40;

    void show() {
        System.out.println(pub + " " + pro + " " + def + " " + pri);
    }
}

5) Packages

  • Package is used to organize classes into folders and avoid name conflicts.
  • Example: package com.webtechio.notes;
  • Import classes using: import package.ClassName;
// File: com/webtechio/notes/App.java
package com.webtechio.notes;

public class App {
    public static void main(String[] args) {
        System.out.println("Package Demo");
    }
}

6) Encapsulation

  • Encapsulation means binding data and methods together and hiding data using private.
  • Use getters and setters to access/update data safely.
class Account {
    private double balance; // hidden data

    public void setBalance(double balance) {
        if (balance >= 0) {
            this.balance = balance;
        }
    }

    public double getBalance() {
        return balance;
    }

    public static void main(String[] args) {
        Account a = new Account();
        a.setBalance(1500.50);
        System.out.println("Balance: " + a.getBalance());
    }
}

7) Abstraction

  • Abstraction means showing only essential details and hiding internal implementation.
  • Achieved using abstract class or interface.
abstract class Payment {
    abstract void pay(double amount); // no body
}

class UpiPayment extends Payment {
    void pay(double amount) {
        System.out.println("Paid via UPI: " + amount);
    }

    public static void main(String[] args) {
        Payment p = new UpiPayment();
        p.pay(499.00);
    }
}

8) static Keyword

  • static belongs to the class (not object).
  • Static members are shared by all objects.
  • Static method can be called without creating an object.
class StaticDemo {
    static String appName = "QuiteXams";

    static void showApp() {
        System.out.println("App: " + appName);
    }

    public static void main(String[] args) {
        StaticDemo.showApp(); // no object needed
    }
}

9) this Keyword

  • this refers to the current object.
  • Used to resolve name conflict, call current class constructor/method, pass current object.
class ThisDemo {
    String name;

    ThisDemo(String name) {
        this.name = name; // current object field = parameter
    }

    void show() {
        System.out.println("Name: " + this.name);
    }

    public static void main(String[] args) {
        ThisDemo t = new ThisDemo("Sourav");
        t.show();
    }
}
⬅ Previous Next ➡