Paramclasses Academy - Blog

Tutorials Details

What is Object-Oriented Programming in Java ?

What is Object-Oriented Programming in Java ?

October 21, 2026

Introduction

Object-Oriented Programming (OOP) is one of the most important concepts in Java programming. It helps developers organize code into reusable and manageable parts.

If you are learning Core Java, understanding OOP will help you build real-world applications such as banking systems, student management systems, e-commerce websites, and employee management software.

In this tutorial, you will learn what OOP is, why Java uses it, its four main pillars, and how to implement OOP concepts with practical Java examples.

What Is Object-Oriented Programming in Java?

Object-Oriented Programming is a programming approach in which programs are designed using classes and objects.

An object represents something that has:

  • State: The data or properties of an object.

  • Behavior: The actions an object can perform.

  • Identity: What distinguishes one object from another.

For example, a car has properties such as color, brand, and speed. It also has behaviors such as starting, stopping, and accelerating.

Java uses classes to define the structure of objects and objects to represent individual instances of those classes.

Real-Life Example of OOP

Imagine a school management system. It may contain different objects:

  • Student: name, roll number, and marks.

  • Teacher: name, subject, and salary.

  • Course: course name, duration, and fees.

Each object has its own data and behavior. OOP helps organize these objects in a structured way.

Why Is OOP Important in Java?

OOP makes software development easier to manage, especially when an application becomes large.

Its main benefits include:

  1. Code reusability: Existing code can be reused through inheritance and composition.

  2. Data protection: Encapsulation helps control access to an object's internal data.

  3. Maintainability: Code organized into classes is often easier to update.

  4. Flexibility: Polymorphism allows common interfaces to work with different implementations.

  5. Modularity: Applications can be divided into smaller, understandable components.

  6. Real-world modeling: Classes and objects can represent real-world entities.

OOP does not automatically make every program better. For small tasks, a simpler approach may be sufficient. However, OOP is especially useful for structured applications.

The Four Pillars of OOP in Java

The four commonly taught pillars of OOP are:

Pillar Meaning Main Purpose
Encapsulation Combining data and methods within a class Control access to data
Inheritance Creating a class based on another class Reuse and specialize behavior
Polymorphism One interface or method name with different behavior Provide flexibility
Abstraction Exposing essential operations while hiding implementation details Reduce complexity

Let's understand each pillar with practical examples.

1. Encapsulation in Java

Encapsulation means keeping data and the methods that operate on that data together in a class while controlling access to the internal state.

In Java, encapsulation is commonly implemented by declaring fields private and providing public methods to access or update them when appropriate.

Real-Life Example

Consider a bank account. A customer should not be able to change the account balance directly to any value. Instead, deposits and withdrawals should follow defined rules.

Java Example

class BankAccount {
    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();

        account.deposit(1000);
        System.out.println(account.getBalance());
    }
}

Output:

1000.0

Explanation

  • private double balance prevents direct access to the field from outside the class.

  • deposit() checks that the amount is positive before adding it.

  • getBalance() allows the balance to be read.

  • The BankAccount object manages its own data according to the rules defined in the class.

Remember: Encapsulation is not just about using getters and setters. It is about controlling access and protecting an object's valid state.

2. Inheritance in Java

Inheritance allows one class to acquire accessible fields and methods from another class. The existing class is called the parent class or superclass, while the new class is called the child class or subclass.

Java uses the extends keyword for class inheritance.

Real-Life Example

A general Animal class might define a behavior such as eating. A Dog class can inherit that behavior and add its own behavior, such as barking.

Java Example

class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();

        dog.eat();
        dog.bark();
    }
}

Output:

Animal is eating
Dog is barking

Explanation

  • Animal is the parent class.

  • Dog extends Animal declares that Dog inherits from Animal.

  • The Dog object can call both eat() and bark().

Types of Inheritance in Java

The common inheritance structures are:

  • Single inheritance: One child class inherits from one parent class.

  • Multilevel inheritance: A class inherits from a class that itself inherits from another class.

  • Hierarchical inheritance: Multiple child classes inherit from the same parent class.

Java classes do not support multiple inheritance from multiple classes. However, a class can implement multiple interfaces.

Important: Use inheritance when there is a meaningful “is-a” relationship. For example, a dog is an animal. For a “has-a” relationship, composition may be more appropriate.

3. Polymorphism in Java

Polymorphism means “many forms.” In Java, it allows the same method name or common reference type to represent different behaviors.

The two commonly taught forms of polymorphism are:

  • Compile-time polymorphism: Commonly achieved through method overloading.

  • Runtime polymorphism: Achieved through method overriding and dynamic method dispatch.

A. Method Overloading

Method overloading means defining multiple methods with the same name but different parameter lists in the same class.

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 Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();

        System.out.println(calculator.add(10, 20));
        System.out.println(calculator.add(10, 20, 30));
    }
}

Output:

30
60

Java selects the appropriate overloaded method based on the arguments supplied.

B. Method Overriding

Method overriding occurs when a subclass provides its own implementation of an inherited instance method, following Java's overriding rules.

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal animal = new Dog();
        animal.sound();
    }
}

Output:

Dog barks

Explanation

Although the reference variable is of type Animal, it refers to a Dog object. At runtime, Java calls the overridden Dog implementation of sound().

This is an example of runtime polymorphism.

Remember: Overloading uses different parameter lists. Overriding provides a subclass-specific implementation of an inherited method.

4. Abstraction in Java

Abstraction means exposing the essential operations of something while hiding unnecessary implementation details.

Java supports abstraction through abstract classes and interfaces.

Real-Life Example

When you use an ATM, you choose an operation such as withdrawing cash. You do not need to understand every internal banking process to use it.

Similarly, abstraction allows a programmer to work with a clear set of operations without needing to know every implementation detail.

Java Example Using an Abstract Class

abstract class Shape {
    abstract double calculateArea();
}

class Circle extends Shape {
    private double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double calculateArea() {
        return Math.PI * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        Shape shape = new Circle(5);

        System.out.printf("%.2f%n", shape.calculateArea());
    }
}

Output:

78.54

Explanation

  • Shape is an abstract class.

  • calculateArea() is an abstract method without a method body.

  • Circle provides the implementation of calculateArea().

  • The program uses a Shape reference without needing to depend directly on the implementation details of Circle.

An abstract class cannot be instantiated directly. It can contain both abstract and concrete methods.

Abstract Class vs Interface

Abstract Class Interface
Declared using abstract class Declared using interface
Can contain instance fields and constructors Cannot have constructors or ordinary per-instance fields
A class can extend only one class A class can implement multiple interfaces
Useful for a shared base with common state or behavior Useful for defining a contract that different classes can implement

Modern Java interfaces can also contain default, static, and private methods, subject to Java's language rules.

OOP Concepts Working Together

In real-world software, the four pillars are often used together.

For example, imagine an online shopping application:

  • Encapsulation: An order controls how its status and item details are updated.

  • Inheritance: Specialized account types may share behavior from a common account class when that relationship makes sense.

  • Polymorphism: Different payment methods can implement a common Payment interface.

  • Abstraction: The checkout process can call pay() without knowing every payment provider's internal implementation.

The result is code that can be organized into components and extended without putting every responsibility into one large class.

Complete Practical Example: OOP in Java

The following example demonstrates encapsulation, inheritance, and runtime polymorphism in one small program.

class Employee {
    private String name;

    Employee(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    void work() {
        System.out.println(name + " is working");
    }
}

class Developer extends Employee {
    Developer(String name) {
        super(name);
    }

    @Override
    void work() {
        System.out.println(getName() + " is writing Java code");
    }
}

public class Main {
    public static void main(String[] args) {
        Employee employee = new Developer("Aman");

        System.out.println(employee.getName());
        employee.work();
    }
}

Output:

Aman
Aman is writing Java code

What Does This Program Demonstrate?

  1. Encapsulation: The name field is private and accessed through getName().

  2. Inheritance: Developer extends Employee.

  3. Polymorphism: The Employee reference points to a Developer object, so the overridden work() method runs.

  4. Abstraction-like design: The code uses the general Employee type to interact with a more specific object. This example does not use an abstract class or interface; those are explicit language mechanisms for defining abstractions.

OOP vs Procedural Programming

OOP Procedural Programming
Organizes programs around objects and classes Organizes programs around procedures or functions
Groups data and related behavior into objects Data and functions may be organized separately
Supports inheritance and polymorphism Does not inherently provide class-based inheritance and polymorphism
Useful for many large, structured applications Useful for small, sequential, or function-oriented tasks

Both approaches can be useful. The right choice depends on the application's requirements.

Common OOP Mistakes Beginners Should Avoid

  1. Confusing classes and objects: A class defines a structure; an object is an instance of that class.

  2. Making every field public: This can expose internal state unnecessarily.

  3. Using inheritance everywhere: Inheritance should represent a meaningful relationship.

  4. Confusing overloading with overriding: Overloading changes the parameter list; overriding changes inherited method behavior.

  5. Thinking abstraction means deleting all details: It means hiding implementation details behind a clear interface.

  6. Forgetting @Override: This annotation helps the compiler detect mistakes when overriding methods.

  7. Assuming OOP automatically makes code secure: Security also depends on validation, access control, design, and other practices.

Frequently Asked Questions (FAQs)

1. What is OOP in Java?

OOP stands for Object-Oriented Programming. It is a programming approach that organizes code around classes and objects.

2. What are the four pillars of OOP?

The four commonly taught pillars are encapsulation, inheritance, polymorphism, and abstraction.

3. Is Java a purely object-oriented programming language?

No. Java supports object-oriented programming, but it also has primitive data types such as int, char, and boolean, along with static members and other non-object features.

4. What is the difference between a class and an object?

A class defines the structure and behavior. An object is an instance created from that class.

5. What is the difference between abstraction and encapsulation?

Abstraction focuses on exposing essential operations while hiding implementation details. Encapsulation controls access to an object's internal data and behavior.

6. Does Java support multiple inheritance?

Java does not allow a class to extend multiple classes. A class can implement multiple interfaces.

7. Why is polymorphism useful?

Polymorphism allows different implementations to be used through a common method or reference type, making programs more flexible and extensible.

8. Where is OOP used in real-world applications?

OOP is used in many types of software, including banking applications, e-commerce systems, desktop applications, enterprise software, and Android applications.

Practice Exercises for Beginners

Try these exercises to test your understanding:

  1. Create a Student class with fields for name and roll number. Create an object and display its details.

  2. Create a Vehicle parent class and a Car child class to demonstrate inheritance.

  3. Create a Calculator class with overloaded multiply() methods.

  4. Create an abstract Payment class with an abstract pay() method and implement it in a child class.

  5. Create an Employee class with a private salary field and a method that validates salary updates.

Conclusion

Object-Oriented Programming is a fundamental part of Core Java. Classes and objects help structure a program, while encapsulation, inheritance, polymorphism, and abstraction provide useful ways to protect data, reuse behavior, support multiple implementations, and hide unnecessary complexity.

Practice each concept with small programs before combining them into larger applications. Understanding these principles will prepare you for advanced Java topics, frameworks, and real-world projects.