Skip to main content
Java Basics
CHAPTER 12 Beginner

Object-Oriented Programming Basics

Updated: May 17, 2026
5 min read

# CHAPTER 12

Object-Oriented Programming Basics

1. Introduction

Object-Oriented Programming (OOP) is the backbone of Java. Instead of writing a long sequence of instructions, OOP organizes code around objects — self-contained units that combine data and behavior. Think of the real world: a Car has properties (color, speed) and behaviors (drive, brake). OOP models software the same way.

2. Learning Objectives

  • Understand the OOP paradigm vs procedural programming.
  • Learn the four pillars of OOP.
  • Create classes and objects conceptually.
  • Relate real-world entities to Java objects.

3. What is OOP?

OOP is a programming paradigm based on the concept of "objects" which contain data (fields/attributes) and code (methods/behavior).

Procedural vs OOP:

ProceduralOOP
Functions operate on dataObjects contain data AND functions
Top-down approachBottom-up approach
Data is exposed globallyData is encapsulated
Hard to maintain at scaleHighly modular and maintainable

4. The Four Pillars of OOP

123456789101112
+------------------+------------------+
|  ENCAPSULATION   |  INHERITANCE     |
|  Wrapping data   |  Child classes   |
|  and methods     |  inherit from    |
|  together,       |  parent classes  |
|  hiding internals|                  |
+------------------+------------------+
|  POLYMORPHISM    |  ABSTRACTION     |
|  One interface,  |  Hiding complex  |
|  many forms      |  details, showing|
|  (method override)|  only essentials |
+------------------+------------------+
  1. 1. Encapsulation: Bundling data + methods together and restricting direct access (using private and getters/setters).
  1. 2. Inheritance: A class inheriting properties and methods from another class (extends).
  1. 3. Polymorphism: The ability of an object to take many forms (method overloading and overriding).
  1. 4. Abstraction: Hiding implementation details and showing only functionality (abstract classes and interfaces).

5. Real-World Analogy: The Car

12345678
Class: Car (Blueprint)
├── Attributes: color, model, speed, fuelLevel
└── Methods: start(), accelerate(), brake(), refuel()

Objects (Instances):
├── myCar = new Car("Red", "Tesla Model 3")
├── yourCar = new Car("Blue", "Honda Civic")
└── taxiCar = new Car("Yellow", "Toyota Camry")

The Class is the blueprint. The Objects are the actual cars built from that blueprint.

6. Classes and Objects Overview

java
1234567891011121314151617
// Class = Blueprint
public class Dog {
    String name;
    String breed;
    int age;
    
    void bark() {
        System.out.println(name + " says: Woof!");
    }
}

// Creating Objects
Dog dog1 = new Dog();
dog1.name = "Buddy";
dog1.breed = "Golden Retriever";
dog1.age = 3;
dog1.bark(); // "Buddy says: Woof!"

7. Benefits of OOP

  • Modularity: Code is organized into independent objects.
  • Reusability: Classes can be reused across projects.
  • Scalability: Easy to add new features without breaking existing code.
  • Maintainability: Changes to one class don't affect others.
  • Security: Data hiding through encapsulation.

8. Common Mistakes

  • Confusing classes with objects: A class is the blueprint; an object is the instance.
  • Not understanding new: new Dog() allocates memory on the Heap.
  • Overcomplicating: Not every program needs OOP. Simple scripts can be procedural.

9. MCQ Quiz with Answers

Question 1

What are the four pillars of OOP?

Question 2

A class is:

Question 3

An object is:

Question 4

Which keyword creates an object?

Question 5

Encapsulation means:

Question 6

OOP stands for:

Question 7

Which is NOT a pillar of OOP?

Question 8

Inheritance allows:

Question 9

Polymorphism means:

Question 10

Where are objects stored in memory?

10. Interview Questions

  • Q: Explain the four pillars of OOP with real-world examples.
  • Q: Why is OOP preferred over procedural programming for large applications?
  • Q: What is the relationship between a class and an object?

11. Summary

OOP models software as a collection of objects that contain data and behavior. The four pillars — Encapsulation, Inheritance, Polymorphism, and Abstraction — provide modularity, reusability, and security. Java is fundamentally an OOP language where everything exists inside classes.

12. Next Chapter Recommendation

In Chapter 13: Classes and Objects, we'll build real Java classes with attributes, methods, access modifiers, and an Employee management system.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·