OOP Concepts in Java

Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 6 min read

Learn encapsulation, inheritance, polymorphism, and abstraction with simple beginner-friendly meaning.

OOP stands for Object-Oriented Programming. It is a programming style where code is organized around classes and objects instead of only writing step-by-step instructions. Java uses OOP to make programs more organized, reusable, and easier to maintain. When a project becomes large, OOP helps divide the program into meaningful parts such as `Student`, `Account`, `Employee`, `Vehicle`, or `Node`.

The four main OOP concepts are encapsulation, inheritance, polymorphism, and abstraction. These are not just interview words; they explain how Java code is designed in real applications.

Encapsulation

Encapsulation means wrapping data and methods together inside a class. The data is stored in fields, and the methods define how that data should be used or changed. Encapsulation also helps protect data from direct unwanted access. In Java, this is usually done by making fields `private` and providing public methods to read or update them in a controlled way.

```java

class Student {

private int marks;

void setMarks(int marks) {

if (marks >= 0) {

this.marks = marks;

}

}

}

``` Here, marks cannot be changed directly from outside the class. The method can check whether the value is valid before storing it.

Inheritance

Inheritance allows one class to reuse properties and methods of another class. The class that gives features is called the parent class, and the class that receives features is called the child class. Inheritance is useful when two classes have an is-a relationship. For example, a `Dog` is an `Animal`, a `Car` is a `Vehicle`, and a `SavingsAccount` is an `Account`.

In Java, inheritance is written using the `extends` keyword. It helps avoid repeated code because common features can stay in the parent class.

Polymorphism

Polymorphism means one name can have different forms. In Java, method overloading and method overriding are common examples. Method overloading happens when methods have the same name but different parameters in the same class. Method overriding happens when a child class gives its own version of a method already present in the parent class.

Polymorphism makes code flexible because the same method name can behave differently based on the object or the arguments.

Abstraction

Abstraction means showing only important details and hiding internal complexity. The user should know what a method does, but does not always need to know how it does it internally. For example, when you use a phone call button, you do not need to know the internal network process. In Java, abstraction can be achieved using abstract classes and interfaces.

Abstraction is useful when we want to define a common rule or behavior and let different classes provide their own internal implementation.

Why OOP Matters

OOP is important because it helps programmers write code that is easier to expand. If tomorrow a project needs a new type of user, product, account, or vehicle, classes and objects make that extension cleaner. For DSA, OOP is also useful in topics like linked lists, trees, graphs, heaps, and custom data structures. For example, a linked list node is commonly represented using a class.

Example Program

```java

class Animal {

void eat() {

System.out.println("Dog 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();

}

}

``` In this program, `Animal` is the parent class and `Dog` is the child class. The `Dog` object can call `bark()` from its own class and `eat()` from the parent class. This shows simple inheritance.

Practice Question

Create a parent class `Vehicle` and a child class `Car`. Add one method in each class, create a `Car` object, and call both methods.

More Java Lessons

Browse all PrepCampus study materials