Java object oriented programming concepts

Java Object Oriented Programming Concepts

April 6th, 2026
2910
5:00 Minutes

Java programming language is among the top 7 with its robustness, scalability and ease of maintenance. An important reason for this is its foundation in Object-Oriented Programming (OOP). Therefore, it has become essential to understand Java object oriented programming concepts for anyone aspiring to build a career in software development.

This article here is a comprehensive guide, exploring what is OOP in Java, its advantages, fundamental concepts and more. It also delves into the core OOP principles with real-world examples.

What is the Java Object-Oriented Programming Concept?

OOP is a programming concept that helps to structure software design around objects instead of functions and logic. In simpler terms, it is a way of structuring programs by bundling related properties and behaviors into individual units. These objects are instances of classes and act as blueprints.

This approach mirrors the real world, where everything can be thought of as an object with specific characteristics and actions. Java object-oriented programming concepts are best for reusability, modularity and better organization of code. These also make large and complex applications more manageable.

Now you might be wondering what are objects and classes in Java. Let's understand them first.

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

Characteristics of Java Object-Oriented Programming Concept Character

Understanding the characteristics of Java OOP is fundamental to grasping all of its concepts. Here is a basic introduction to each of them:

1. Classes in Java

Think of a class in Java as a blueprint or a template that can create objects. It defines a set of attributes and methods that all objects of that class will possess. Think of a Car class defining all cars that will have color, make, model (attributes) and can start(), accelerate(), brake() (methods). A class itself does not occupy memory as it is just a definition.


public class Car {

    // Attributes (variables)
    String make;
    String model;
    String color;
    int year;

    // Behaviors (methods)
    public void start() {
        System.out.println("The " + make + " " + model + " is starting.");
    }

    public void accelerate() {
        System.out.println("The " + make + " " + model + " is accelerating.");
    }
}

2. Objects in Java

An object is an instance of a class. When you create an object, you are essentially bringing the blueprint to life. These objects have their own particular set of attribute values. Using the Car class example, myCar = new Car(); will create an actual car object. This object will occupy memory and can have specific values like myCar.color = "Red"; or myCar.make = "Toyota";.


public static void main(String[] args) {
    // Creating an object (instance) of the Car class
    Car myCar = new Car();

    // Setting attributes of the object
    myCar.make = "Honda";
    myCar.model = "Civic";
    myCar.color = "Blue";
    myCar.year = 2023;

    // Calling behaviors (methods) of the object
    myCar.start();
    myCar.accelerate();
}

3. Encapsulation

Encapsulation is the mechanism of bundling the data and the methods that operate on the data into a single unit, or class. It is often described as data hiding where the internal state of an object is protected from direct external access. Here access to the data is usually controlled through public methods like getters and setters. This protects the integrity of the data and makes the code more robust.

Example:


public class BankAccount {
    private String accountNumber; // Data is private
    private double balance; // Data is private

    public BankAccount(String accountNumber, double initialBalance) {
        this.accountNumber = accountNumber;
        this.balance = initialBalance;
    }

    // Public method to get the balance (controlled access)
    public double getBalance() {
        return balance;
    }

    // Public method to deposit (controlled access)
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Deposited: INR" + amount + ". New balance: INR" + balance);
        } else {
            System.out.println("Deposit amount must be positive.");
        }
    }

    // Public method to withdraw (controlled access)
    public void withdraw(double amount) {
        if (amount > 0 && balance >= amount) {
            balance -= amount;
            System.out.println("Withdrew: INR" + amount + ". New balance: INR" + balance);
        } else {
            System.out.println("Invalid withdrawal amount or insufficient balance.");
        }
    }

    public static void main(String[] args) {
        BankAccount myAccount = new BankAccount("SBIN0012345", 1000.0);
        // Accessing data through methods (encapsulated)
        System.out.println("Initial Balance: INR" + myAccount.getBalance());
        myAccount.deposit(500.0);
        myAccount.withdraw(200.0);
        myAccount.withdraw(2000.0); // Invalid withdrawal
        // myAccount.balance = -500; // This would be prevented by encapsulation if 'balance' was private
    }
}

4. Abstraction

Abstraction hides the complicated implementation details and highlights the essential features of the object. It only focuses on what an object is doing rather than how it is doing. Abstraction in Java is generally achieved by using abstract interfaces and classes. It helps experts in defining a general interface for a suite of related objects.

Example:


// Abstract class
abstract class Shape {
    String color;
    // Abstract method (no implementation)
    abstract double calculateArea();
    // Concrete method
    public void displayColor() {
        System.out.println("This shape is " + color);
    }
}

// Concrete subclass
class Circle extends Shape {
    double radius;
    public Circle(String color, double radius) {
        this.color = color;
        this.radius = radius;
    }
    @Override
    double calculateArea() {
        return Math.PI * radius * radius;
    }
}

// Concrete subclass
class Rectangle extends Shape {
    double length;
    double width;
    public Rectangle(String color, double length, double width) {
        this.color = color;
        this.length = length;
        this.width = width;
    }
    @Override
    double calculateArea() {
        return length * width;
    }
}

public class AbstractionDemo {
    public static void main(String[] args) {
        Shape circle = new Circle("Red", 5.0);
        Shape rectangle = new Rectangle("Blue", 4.0, 6.0);
        // We only care about calculating area and displaying color, not the internal calculation
        System.out.println("Circle Area: " + circle.calculateArea());
        circle.displayColor();
        System.out.println("Rectangle Area: " + rectangle.calculateArea());
        rectangle.displayColor();
    }
}

5. Polymorphism

Polymorphism means many forms. It makes sure that objects of different classes are always treated as objects of the same type. It also enables a single interface to represent different underlying forms. Polymorphism is generally achieved by the following methods:

Method Overloading: These are multiple methods with the same name and different parameters available in the same class.

Method Overriding: A subclass provides a specific implementation for a method that is already defined in its superclass.

Example:


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

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

class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat meows");
    }
}

public class PolymorphismDemo {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        Animal myDog = new Dog(); // Dog object treated as Animal type
        Animal myCat = new Cat(); // Cat object treated as Animal type
        myAnimal.makeSound(); // Output: Animal makes a sound
        myDog.makeSound(); // Output: Dog barks (Runtime polymorphism - specific implementation of Dog is called)
        myCat.makeSound(); // Output: Cat meows (Runtime polymorphism - specific implementation of Cat is called)
    }
}

6. Inheritance

Inheritance is a fundamental OOP concept that allows a class to inherit properties and behaviors from another class. It establishes an is-a relationship that promotes code reusability and forms a hierarchy of classes. The extends keyword is mostly used for inheritance in Java.

Example:


class Animal { // Superclass
    String species;
    public Animal(String species) {
        this.species = species;
    }
    public void eat() {
        System.out.println(species + " is eating.");
    }
    public void sleep() {
        System.out.println(species + " is sleeping.");
    }
}

class Dog extends Animal { // Subclass, inherits from Animal
    String breed;
    public Dog(String breed) {
        super("Canine"); // Call superclass constructor
        this.breed = breed;
    }
    public void bark() {
        System.out.println("Woof woof!");
    }
    public static void main(String[] args) {
        Dog myDog = new Dog("Golden Retriever");
        System.out.println("My dog is a " + myDog.breed + " " + myDog.species);
        myDog.eat(); // Inherited method
        myDog.sleep(); // Inherited method
        myDog.bark(); // Dog's own method
    }
}

Advantages of Using Java Object-Oriented Programming Concepts

You may have also wondered how the use of Java object-oriented programming concepts could be beneficial. Well, it provides a variety of benefits including:

  • Modularity: OOP promotes breaking down complex problems into smaller and manageable objects. Each object can be developed and tested independently to simplify the overall development process.
  • Reusability: Once an object or class is created, it can be reused in different parts of the program or even in other projects. This reduces development time and effort.
  • Maintainability: The modular nature of object-oriented programming makes code easier to maintain and debug. Here any change in a single part of the system will often affect other parts, as objects are self-contained.
  • Scalability: OOP allows for easy expansion without major restructuring. New functionalities can often be added by creating new objects or extending existing ones.
  • Security or Data Hiding: Concepts like encapsulation allow for data hiding, protecting sensitive information from unauthorized access and modification to improve the app security.
  • Improved Collaboration: With its well-defined object structure, multiple developers can work on different parts of the same project simultaneously with minimal conflicts.

Read Also- How to Learn Java from Scratch

Object Relationships and Design Principles in Java

Beyond the four foundational pillars of OOP, Java also use several crucial concepts that define how objects interact and form more complex systems. These principles often referred to as object relationships or design principles. these are essential for building modular, reusable and maintainable applications. Let's delve into these important concepts that govern how objects connect and collaborate. 

1. Composition

Composition is basically a has-a relationship between two different classes where one of the classes contains an object of another class. It is a strong form of connection where the stored object can not ever exist independently of the containing object. If the containing object is destroyed, the contained object is also destroyed.

Example:


class Engine {
    String type;
    public Engine(String type) {
        this.type = type;
        System.out.println(type + " Engine created.");
    }
    public void start() {
        System.out.println(type + " Engine started.");
    }
}

class CarWithComposition {
    private Engine engine; // Car has an Engine
    public CarWithComposition(String engineType) {
        this.engine = new Engine(engineType); // Engine is created when Car is created
    }
    public void startCar() {
        engine.start(); // Car delegates starting to its Engine
        System.out.println("Car is ready to drive.");
    }
    // When CarWithComposition object is destroyed, Engine object also ceases to exist conceptually
    // in this tightly coupled relationship.
    public static void main(String[] args) {
        CarWithComposition myLuxuryCar = new CarWithComposition("V8");
        myLuxuryCar.startCar();
    }
}

2. Aggregation

Aggregation is also a has-a relationship, but it is a weaker form of association than composition. In aggregation, the contained object can exist independently of the containing object. If the containing object is destroyed, the contained object is not necessarily destroyed.

Example:


class Professor {
    String name;
    String specialty;
    public Professor(String name, String specialty) {
        this.name = name;
        this.specialty = specialty;
    }
    public void teach() {
        System.out.println(name + " is teaching " + specialty + ".");
    }
}

class Department {
    String departmentName;
    // Aggregation: Department has a list of Professors.
    // Professors can exist independently of the Department.
    private List<Professor> professors;
    public Department(String departmentName) {
        this.departmentName = departmentName;
        this.professors = new ArrayList<>();
    }
    public void addProfessor(Professor prof) {
        this.professors.add(prof);
        System.out.println(prof.name + " added to " + departmentName + " department.");
    }
    public void listProfessors() {
        System.out.println("Professors in " + departmentName + " department:");
        for (Professor prof : professors) {
            System.out.println("- " + prof.name + " (" + prof.specialty + ")");
        }
    }
    public static void main(String[] args) {
        Professor prof1 = new Professor("Dr. Sharma", "Computer Science");
        Professor prof2 = new Professor("Dr. Khan", "Mathematics");
        Department compSciDept = new Department("Computer Science");
        compSciDept.addProfessor(prof1);
        compSciDept.addProfessor(prof2);
        compSciDept.listProfessors();
        // If 'compSciDept' object is removed, 'prof1' and 'prof2' still exist as objects
        // and can be associated with other departments or exist independently.
    }
}

3. Association

Association is a very general term representing any relationship between two or more independent classes. It implies that two classes are connected, but neither owns the other and they can exist independently. It can be one-to-one, one-to-many, many-to-one or many-to-many. Both composition and aggregation are specific types of association.

Example:


// Example for a simple association: Student and Course
class Student {
    String name;
    long studentId;
    public Student(String name, long studentId) {
        this.name = name;
        this.studentId = studentId;
    }
    public void attends(Course course) {
        System.out.println(name + " (ID: " + studentId + ") attends " + course.courseName);
    }
}

class Course {
    String courseName;
    String courseCode;
    public Course(String courseName, String courseCode) {
        this.courseName = courseName;
        this.courseCode = courseCode;
    }
    public void describeCourse() {
        System.out.println("Course: " + courseName + " (" + courseCode + ")");
    }
}

public class AssociationDemo {
    public static void main(String[] args) {
        Student john = new Student("John Doe", 1001);
        Student jane = new Student("Jane Smith", 1002);
        Course javaCourse = new Course("Java Programming", "CS101");
        Course mathsCourse = new Course("Calculus I", "MA201");
        // Students are associated with Courses
        john.attends(javaCourse);
        jane.attends(mathsCourse);
        // Courses can also be described independently
        javaCourse.describeCourse();
    }
}

Wrapping Up Java Object-Oriented Programming Concepts

Understanding Java object-oriented programming concepts is undeniably an essential for any serious Java developer. With its core principles like encapsulation, abstraction, polymorphism, inheritance, composition, aggregation, and association, one can be a proficient developer. It makes them able to write not only functional but also highly organized, reusable, maintainable and scalable programs.

FAQs for OOP Concepts in Java

Q1. What are the wrapper classes in Java?

Wrapper classes in Java are basically a part of the java.lang package. They are responsible for providing a way to use primitive data types like int, char, boolean or double as objects. Each primitive type has a corresponding wrapper class and helps to use different utility methods.

Q2. What is an optional class in Java 8?

It is a special type of container object that may or may not contain a non-null value. It is often used to represent the absence of a value in a more explicit and type-safe way. This helps to avoid NullPointerException errors.

Q3. What is the use of a wrapper class in Java?

Wrapper classes convert Java elements into objects to enable their use in collections, serialization and synchronization. They also provide utility methods for type conversions. This simplifies the data handling in an object-oriented context.

Q4. Why is OOP important in Java?

OOP helps organize code in a clear and structured way. It makes Java programs easier to maintain, reuse and scale.

Q5. Is OOP easy for freshers to learn?

Yes, OOP concepts are easy with practice. They are widely used in real-world Java applications.

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled content writer with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.