DEV Community

Cover image for Java OOP Explained With Simple Examples
Rachit Joshi
Rachit Joshi

Posted on

Java OOP Explained With Simple Examples

Understand classes, objects, inheritance, polymorphism, encapsulation, and abstraction with easy Java examples.

If you're learning Java, one topic you'll come across again and again is Object-Oriented Programming (OOP).

At first, terms like inheritance, polymorphism, abstraction, and encapsulation can sound complicated. But once you understand the basic idea behind each concept and write a few simple programs, OOP becomes much easier to work with.

In this guide, we'll break down the main OOP concepts in Java with simple examples and see how they fit together.

What Is Object-Oriented Programming?

Object-Oriented Programming is a programming approach where a program is designed around objects rather than just a collection of functions and instructions.

An object can contain:

Data — information about the object
Methods — actions the object can perform

For example, think about a Student.

A student might have:

Name
Age
Course

And the student might perform actions such as:

Attend a class
Submit an assignment
Take an exam

Java uses classes and objects to represent these kinds of entities in code.

1. Classes and Objects

A class can be thought of as a blueprint. An object is an actual instance created from that blueprint.

Here's a simple example:

class Student {
String name;

void introduce() {
    System.out.println("My name is " + name);
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {

    Student student = new Student();

    student.name = "Rahul";
    student.introduce();
}
Enter fullscreen mode Exit fullscreen mode

}

Here, Student is the class.

The following line creates an object:

Student student = new Student();

The object can then access the variables and methods defined in the class.

Why Are Classes Useful?

Instead of keeping data and related functions separately, a class lets you organize them together.

This becomes especially useful when applications become larger.

2. Encapsulation

Encapsulation means keeping data and the methods that operate on it together while controlling how the data can be accessed.

For example:

class BankAccount {

private double balance;

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

public double getBalance() {
    return balance;
}
Enter fullscreen mode Exit fullscreen mode

}

The balance variable is marked as private.

That means other classes cannot directly modify it.

Instead, they can interact with it through methods such as deposit() and getBalance().

Why Use Encapsulation?

Encapsulation can help you:

Protect important data
Control how data is modified
Keep code organized
Make classes easier to maintain

3. Inheritance

Inheritance allows one class to reuse properties and methods from another class.

For example:

class Animal {

void eat() {
    System.out.println("Animal is eating");
}
Enter fullscreen mode Exit fullscreen mode

}

class Dog extends Animal {

void bark() {
    System.out.println("Dog is barking");
}
Enter fullscreen mode Exit fullscreen mode

}

Here, Dog extends Animal.

Therefore, a Dog object can use the eat() method inherited from Animal.

public class Main {
public static void main(String[] args) {

    Dog dog = new Dog();

    dog.eat();
    dog.bark();
}
Enter fullscreen mode Exit fullscreen mode

}

Inheritance is useful when different classes share common characteristics or behavior.

4. Polymorphism

The word polymorphism means "many forms."

In Java, polymorphism allows the same method or interface to behave differently depending on the object involved.

One common example is method overriding.

class Animal {

void sound() {
    System.out.println("Animal makes a sound");
}
Enter fullscreen mode Exit fullscreen mode

}

class Dog extends Animal {

@Override
void sound() {
    System.out.println("Dog barks");
}
Enter fullscreen mode Exit fullscreen mode

}

The Dog class provides its own implementation of the sound() method.

Now:

Dog dog = new Dog();
dog.sound();

The output will be:

Dog barks

The same method name, sound(), can therefore have different implementations in different classes.

5. Abstraction

Abstraction means focusing on what an object does while hiding unnecessary implementation details.

Java supports abstraction using abstract classes and interfaces.

Here's a simple abstract class example:

abstract class Vehicle {

abstract void start();
Enter fullscreen mode Exit fullscreen mode

}

class Car extends Vehicle {

@Override
void start() {
    System.out.println("Car starts");
}
Enter fullscreen mode Exit fullscreen mode

}

The Vehicle class says that a vehicle should have a start() method, but it doesn't define exactly how that method works.

The Car class provides the implementation.

This allows you to focus on the important behavior without exposing every implementation detail.

Conclusion

Object-Oriented Programming is one of the most important concepts to understand when learning Java.

Start with classes and objects, then gradually understand encapsulation, inheritance, polymorphism, and abstraction.

Don't worry if these concepts seem confusing at first. Write small programs, experiment with the examples, and learn from the errors you encounter.

The more you practice designing your own classes and objects, the more naturally Java OOP concepts will start to make sense.

Top comments (0)