Java OOP


Object-Oriented Programming (OOP) in Java is one of the most important concepts that makes the language powerful, organized, and reusable. OOP allows you to structure your programs around objects rather than actions, and around data rather than logic. In simple terms, it focuses on combining data and behavior into one single unit — an object.

In this tutorial, you’ll understand what OOP is, why it’s important in Java, and what its main principles are. You’ll also see how OOP concepts make real-world programming easier and more practical.

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming approach where everything revolves around objects. These objects are instances of classes, and they represent real-world entities like a person, car, employee, or bank account.

Each object has two main components:

  • Attributes (Data): Represent the properties of an object.

  • Methods (Functions): Represent the actions or behaviors that the object can perform.

For example, think of a Car object.

  • Attributes: color, brand, speed.

  • Methods: start(), stop(), accelerate().

So, instead of writing one large program that performs all actions, you divide it into small, manageable objects that interact with each other. This approach makes programs more modular, flexible, and easier to maintain.

Why Java is an Object-Oriented Language

Java is built entirely around the OOP model. Everything in Java, except primitive data types (like int, float, char), is treated as an object. Java allows developers to design applications using real-world modeling, which helps in understanding the system easily.

The main reasons Java is considered an OOP language include:

  1. Class-based structure: Every piece of code resides inside a class.

  2. Encapsulation of data: Data and code are bound together.

  3. Reusability: Classes and objects can be reused in other programs.

  4. Inheritance support: New classes can inherit properties from existing ones.

  5. Polymorphism: A single interface can represent different underlying forms.

Main Principles of OOP in Java

There are four main pillars of OOP in Java — Encapsulation, Inheritance, Polymorphism, and Abstraction. Let’s understand each one in simple terms.

1. Encapsulation

Encapsulation means wrapping data (variables) and methods (functions) into a single unit. It restricts direct access to some of the object’s components, which helps protect data from unwanted modification.

For example:

public class Student {
    private String name;  // private variable

    // public method to access private variable
    public void setName(String n) {
        name = n;
    }

    public String getName() {
        return name;
    }
}

Here, the name variable is private, and it can only be accessed through the setName() and getName() methods. This is encapsulation.

2. Inheritance

Inheritance allows one class to acquire the properties and methods of another class. This promotes code reuse and establishes a relationship between parent and child classes.

For example:

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

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

Here, the Dog class inherits the eat() method from the Animal class. So, an object of Dog can access both eat() and bark() methods.

3. Polymorphism

Polymorphism means “many forms.” It allows one method to behave differently depending on the object that calls it.

For example:

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

class Cat extends Animal {
    void sound() {
        System.out.println("Cat meows");
    }
}

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

If you create an Animal reference and assign it to a Dog object, it will call the dog’s version of the sound() method. This is runtime polymorphism.

4. Abstraction

Abstraction is the process of hiding the implementation details and showing only the essential features. In Java, abstraction is achieved using abstract classes and interfaces.

For example:

abstract class Vehicle {
    abstract void start();
}

class Bike extends Vehicle {
    void start() {
        System.out.println("Bike starts with kick");
    }
}

Here, Vehicle defines an abstract method start() without implementation. The Bike class provides the actual implementation. This way, the user focuses only on what the object does, not how it does it.

Benefits of OOP in Java

  1. Reusability: Once a class is created, it can be reused in multiple programs.

  2. Maintainability: Code is easier to maintain and modify.

  3. Modularity: Programs are divided into smaller, independent parts.

  4. Security: Data hiding prevents unauthorized access.

  5. Flexibility: OOP supports polymorphism, which allows one interface to handle different data types.

  6. Real-world mapping: It mirrors real-life entities, making program design more natural.

Real-Life Example of OOP

Let’s imagine an example related to a simple Bank Management System.

We can have the following classes:

  • Account – represents a bank account (balance, account number).

  • Customer – represents account holders (name, contact, address).

  • Transaction – handles deposit and withdrawal operations.

Each class has its own data and methods. The Account class could have methods like deposit() and withdraw(). The Customer class can link multiple accounts. This structure mirrors how banks work in the real world, making the software both realistic and easy to manage.

Difference Between Procedural and Object-Oriented Programming

Feature Procedural Programming Object-Oriented Programming
Approach Focuses on functions Focuses on objects
Data Handling Data is separate from functions Data and methods are combined
Reusability Limited High due to inheritance
Example C language Java
Maintenance Harder to modify Easier to maintain

Summary of the Tutorial

In this tutorial, you learned the basics of Object-Oriented Programming in Java. You understood how OOP organizes code around real-world objects and why it’s the foundation of Java’s design. The four key principles — encapsulation, inheritance, polymorphism, and abstraction — make Java programs more secure, modular, and reusable.

Mastering OOP is the first and most essential step in learning Java development, as every concept you’ll learn next — classes, objects, constructors, modifiers — builds on this foundation.


Practice Questions

  1. Create a class called Student with attributes name, age, and grade. Write methods to input and display student details using objects.

  2. Write a Java program to create a Car class with methods start(), accelerate(), and stop(). Create multiple objects to demonstrate how each object can call these methods.

  3. Create a class hierarchy with a superclass Animal and subclasses Dog, Cat, and Bird. Each subclass should override a method makeSound() to print its own sound.

  4. Demonstrate encapsulation by creating a BankAccount class with a private variable balance. Provide deposit() and withdraw() methods with validation.

  5. Write a Java program that shows the use of inheritance. Create a base class Employee and a derived class Manager that adds new attributes and methods.

  6. Implement abstraction using an abstract class Shape with an abstract method area(). Create subclasses Circle and Rectangle that provide their own implementations of the area() method.

  7. Demonstrate polymorphism by creating a parent class Vehicle and subclasses Car and Bike. Each subclass should override a method run(). Create an array of Vehicle objects and call the run() method for each.

  8. Create a class Person with private attributes name and age. Add getter and setter methods to access them. Create multiple Person objects and display their data.

  9. Write a Java program to show the concept of constructors in OOP. Create a class Book with a parameterized constructor to initialize title and author.

  10. Develop a small real-world OOP example: Create a Library system with classes Book, Member, and Library. Each class should have attributes and methods that represent their behavior (like borrow, return, addBook, etc.).


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top