OOP Concepts in Java - Object, Class and Four Pillars

⏱️ 8 min read • Beginner Level • Lesson 43

Lesson 43 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

Object-Oriented Programming, commonly called OOP, is a programming style where we solve problems by creating objects. Java is strongly based on OOP concepts such as class, object, encapsulation, abstraction, inheritance, and polymorphism.

Before learning OOP concepts, you should understand Java variables, Java methods, and Java data types.


What is OOP in Java?

OOP stands for Object-Oriented Programming. It is a programming approach where a program is designed using objects. An object combines data and behavior into a single unit.

  • Java supports object-oriented programming.

  • OOP helps organize code using classes and objects.

  • It makes code reusable, secure, flexible, and easier to maintain.

OOP Concepts in Java

Why use OOP?

Think of a real-world school system. A student has data such as name, roll number, and marks. A student also has actions such as study, attend exam, and show result. In OOP, we can represent this real-world student as an object.

OOP helps in:
  • Representing real-world entities in code.
  • Reusing code through inheritance.
  • Protecting data using encapsulation.
  • Hiding complex details using abstraction.
  • Writing flexible code using polymorphism.

Real-World Example of OOP

Consider a banking application.

  • Class: BankAccount
  • Objects: JohnAccount, SarahAccount
  • Encapsulation: Balance is protected.
  • Abstraction: Users only see deposit and withdraw options.
  • Inheritance: SavingsAccount inherits BankAccount.
  • Polymorphism: Different accounts calculate interest differently.

OOP vs Procedural Programming

Procedural Programming Object-Oriented Programming
Focuses on functions Focuses on objects
Less secure More secure through encapsulation
Less reusable Highly reusable
Difficult for large projects Suitable for large applications

Object in Java

An object is a real-world entity that has state and behavior.

For example, a laptop can be treated as an object:

State / Data Behavior / Action
Brand, RAM, Storage, Color Start, Shutdown, Run Program
Simple Definition: An object is an instance of a class.

Class in Java

A class is a blueprint or template used to create objects. It defines what data and methods an object will have.

For example, Student can be a class, and Ayan, Sarah, and John can be objects of that class.

Class and Object Example in Java

The following example creates a Student class and uses it to create an object.

StudentExample.java
Copy Try Download
class Student {
    String name;
    int rollNumber;

    void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Roll Number: " + rollNumber);
    }
}

public class StudentExample {
    public static void main(String[] args) {
        Student s1 = new Student();

        s1.name = "Ayan";
        s1.rollNumber = 101;

        s1.displayDetails();
    }
}

Output:

Name: Ayan
Roll Number: 101

Explanation

  1. Student is a class.
  2. name and rollNumber are data members.
  3. displayDetails() is a method.
  4. s1 is an object of the Student class.
  5. The object stores data and calls the method to display details.

Four Pillars of OOP in Java

Java OOP is mainly based on four important concepts:

1. Encapsulation

Wrapping data and methods together and protecting data from direct access.

2. Abstraction

Hiding internal details and showing only essential features.

3. Inheritance

Acquiring properties and methods of one class into another class.

4. Polymorphism

Performing one action in different ways.

Encapsulation in Java

Encapsulation means binding data and methods together inside a class. It also helps protect data by restricting direct access from outside the class.

A common way to achieve encapsulation in Java is by using private variables and public getter/setter methods.

Real-Life Example: An ATM machine protects your bank account balance. You cannot access the balance directly. You can only perform controlled operations like withdraw, deposit, or check balance using valid authentication.
In Java: Encapsulation is usually achieved by declaring variables as private and providing controlled access through getter and setter methods.

Abstraction in Java

Abstraction means hiding implementation details and showing only important information to the user.

For example, when you drive a car, you use the steering wheel, accelerator, and brake. You do not need to know the internal engine working every time you drive.

Real-Life Example: When you drive a car, you use the steering wheel, brake, and accelerator. You do not need to know how the engine, gearbox, or fuel system works internally.
In Java: helps hide complex implementation details from the user. Abstraction can be achieved using abstract classes and interfaces.

Inheritance in Java

Inheritance allows one class to acquire the properties and methods of another class. It supports code reusability.

For example, Dog can inherit common properties from an Animal class.

Real-Life Example: A child inherits features from parents. Similarly, in Java, a child class can inherit properties and methods from a parent class.
In Java: Inheritance is implemented using the extends keyword. It helps in code reuse and creating parent-child relationships between classes.

Polymorphism in Java

Polymorphism means many forms. In Java, it allows one action to behave differently depending on the object or situation.

Real-Life Example: The word "sound" can behave differently depending on the object. A dog barks, a cat meows, and a cow moos. The same action has different forms.
In Java: Polymorphism can be achieved through method overloading and method overriding.
What Next?

In the upcoming lessons, you will learn each OOP concept separately: Class and Object → Encapsulation → Inheritance → Polymorphism → Abstraction.

OOP Concepts Summary Table

Concept Meaning Real-Life Example
Object Entity with state and behavior Student, Laptop, Car
Class Blueprint for creating objects Student class for many student objects
Encapsulation Wrapping data and methods together Bank account with private balance
Abstraction Hiding internal details Using ATM without knowing internal process
Inheritance One class acquiring features of another Dog inherits from Animal
Polymorphism One action with many forms Different animals make different sounds
Interview Question:

What are the four pillars of OOP?

Encapsulation, Abstraction, Inheritance, and Polymorphism.

Summary:
  • OOP stands for Object-Oriented Programming.
  • Java supports OOP using classes and objects.
  • An object has state and behavior.
  • A class is a blueprint for creating objects.
  • The four main pillars of OOP are encapsulation, abstraction, inheritance, and polymorphism.
  • OOP helps make programs reusable, secure, flexible, and easier to maintain.

Interview Questions ⭐

OOP in Java stands for Object-Oriented Programming. It is a programming approach based on objects and classes.

An object is an entity that has state and behavior. It is an instance of a class.

A class is a blueprint or template used to create objects.

The four pillars of OOP in Java are encapsulation, abstraction, inheritance, and polymorphism.

OOP helps make Java programs reusable, secure, modular, flexible, and easier to maintain.

A class is a blueprint, while an object is a real instance created from that blueprint.

Next step: Learn Java Class and Objects

🚀 Continue to Java Class and Objects →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Oops-overview | Language: Java

Question 1 of 10
Q1. What will be the output of this code?
class Student {
    String name = "Ayan";

    void display() {
        System.out.println(name);
    }
}

public class Test {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.display();
    }
}
Q2. What is encapsulation in Java?
Q3. What is abstraction in Java?
Q4. What does OOP stand for in Java?
Q5. What is a class in Java?
Q6. Which statement is TRUE about class and object?
Q7. What is inheritance in Java?
Q8. What does polymorphism mean in Java?
Q9. Which of the following best describes an object in Java?
Q10. Which of the following are the four main pillars of OOP in Java?

🎉 Great job! Continue learning Java OOP step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.