We’ll learn it in the correct order because the concepts depend on each other:
Java OOP
│
├── 1. Class
├── 2. Object
├── 3. Constructor
├── 4. this keyword
├── 5. Instance vs static
├── 6. Encapsulation
├── 7. Inheritance
├── 8. Polymorphism
└── 9. Abstraction
- Class Definition
A class is a blueprint/template that defines the properties and behaviors that objects can have.
A class itself is not usually the actual thing. It describes what an object should contain and do.
Real-time example
Think about a Car.
A car has:
Properties:
color
brand
speed
model
Behaviors:
start()
stop()
accelerate()
brake()
We can represent that with a Java class:
class Car {
String color;
String brand;
int speed;
void start() {
System.out.println("Car started");
}
void stop() {
System.out.println("Car stopped");
}
void accelerate() {
speed += 10;
System.out.println("Speed: " + speed);
}
}
Here:
Car
│
├── Properties
│ ├── color
│ ├── brand
│ └── speed
│
└── Behaviors
├── start()
├── stop()
└── accelerate()
The class is basically a design/template.
- Object Definition
An object is an actual instance of a class.
This distinction is extremely important:
Class → Blueprint
Object → Actual thing created from blueprint
Real-time example
Think about a building blueprint.
Blueprint
↓
HOUSE
From that blueprint, you can build multiple houses:
HOUSE BLUEPRINT
↓
┌────────┼────────┐
↓ ↓ ↓
House A House B House C
Similarly:
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
Here:
Car
↓
Class
car1
car2
car3
↓
Objects
Each object can have its own values.
car1.color = "Red";
car1.brand = "Toyota";
car2.color = "Blue";
car2.brand = "BMW";
Now:
car1
┌────────────────┐
│ color = Red │
│ brand = Toyota │
└────────────────┘
car2
┌────────────────┐
│ color = Blue │
│ brand = BMW │
└────────────────┘
Both are Car objects, but their data is different.
- What does new mean?
You will see this constantly in Java:
Car car = new Car();
The important part is:
new Car()
It creates a new object of the Car class.
Conceptually:
Car class
↓
new Car()
↓
Object created
↓
car reference
So:
Car car = new Car();
means:
Create a new Car object and store a reference to it in car.
- Calling object methods
Once you have an object:
Car car = new Car();
you can access its properties and methods using:
.
called the dot operator.
Example:
car.color = "Red";
car.start();
car.accelerate();
The flow is:
car
↓
.
↓
start()
or:
car
↓
.
↓
color
-
Complete example
class Car {String color;
String brand;
int speed;void start() {
System.out.println("Car started");
}void accelerate() {
speed += 10;
System.out.println("Speed: " + speed);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.color = "Red";
car.brand = "Toyota";
System.out.println(car.color);
System.out.println(car.brand);
car.start();
car.accelerate();
car.accelerate();
}
}
Output:
Red
Toyota
Car started
Speed: 10
Speed: 20
- Constructor
Now we come to another very important concept.
Definition
A constructor is a special member of a class that is automatically called when an object is created.
Example:
Car car = new Car();
When new Car() executes, Java calls the constructor.
Why do we need constructors?
Suppose you have:
Car car = new Car();
car.color = "Red";
car.brand = "Toyota";
car.speed = 0;
That's repetitive.
Instead, we can initialize the object when creating it.
Car car = new Car("Red", "Toyota");
Create the constructor:
class Car {
String color;
String brand;
Car(String color, String brand) {
this.color = color;
this.brand = brand;
}
}
Now:
Car car = new Car("Red", "Toyota");
creates an object that is already initialized.
- Constructor rules
A constructor:
Has the same name as the class
class Car {
Car() {
}
}
Class:
Car
Constructor:
Car()
Has no return type
Correct:
Car() {
}
Incorrect:
void Car() {
}
The second one is actually a method, not a constructor.
- Real-time constructor example
Imagine creating users in an application.
Without a constructor:
User user = new User();
user.name = "John";
user.email = "john@gmail.com";
user.age = 25;
With a constructor:
User user = new User(
"John",
"john@gmail.com",
25
);
Class:
class User {
String name;
String email;
int age;
User(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
}
This is much closer to how you'll see objects initialized in real Java applications.
- The this keyword
This is another important concept.
Look at:
class User {
String name;
User(String name) {
this.name = name;
}
}
We have two names:
name
↓
constructor parameter
this.name
↓
object's variable
So:
this.name = name;
means:
Assign the constructor parameter name to the current object's name property.
Real-time analogy
Imagine two labels:
Object's name
↓
this.name
Incoming value
↓
name
So:
this.name = name;
means:
object's name ← incoming name
- Why is this called "current object"?
Suppose:
User user1 = new User("Alice");
User user2 = new User("Bob");
When constructing user1:
this → user1
When constructing user2:
this → user2
So this always means:
The current object whose method or constructor is executing.
- Instance variables
Consider:
class Student {
String name;
int age;
}
name and age belong to each individual object.
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Alice";
s2.name = "Bob";
Memory conceptually looks like:
s1
┌─────────────┐
│ name=Alice │
│ age=0 │
└─────────────┘
s2
┌─────────────┐
│ name=Bob │
│ age=0 │
└─────────────┘
These are called instance variables because every object gets its own instance data.
- static
Now something different.
Suppose every Student belongs to the same school.
You don't want every object to maintain a separate copy of:
schoolName
Instead:
class Student {
String name;
static String schoolName = "ABC School";
}
Now:
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Alice";
s2.name = "Bob";
Both share:
schoolName = ABC School
Conceptually:
Student class
│
schoolName = ABC
│
┌───────┴───────┐
↓ ↓
s1 s2
Alice Bob
- Instance vs Static
This distinction is important for interviews and real Java development.
Instance
Belongs to an object.
String name;
Access:
student.name
Static
Belongs to the class.
static String schoolName;
Access:
Student.schoolName
Easy way to remember:
instance → each object has its own copy
static → shared at class level
- Static method
You can also have static methods.
class MathUtil {
static int add(int a, int b) {
return a + b;
}
}
You don't need to create an object:
int result = MathUtil.add(10, 20);
Instead of:
MathUtil obj = new MathUtil();
obj.add(...);
This is useful for utility-type operations.
- Now the actual OOP pillars
Once you understand:
Class
Object
Constructor
this
Instance
static
you're ready for the four major OOP concepts.
OOP
│
┌─────────┼─────────┐
↓ ↓ ↓
Encapsulation Inheritance Polymorphism
│
Abstraction
The next concept is Encapsulation.
- Encapsulation Definition
Encapsulation is the practice of bundling data and the methods that operate on that data inside a class, while controlling direct access to the data.
The key idea is:
Don't allow outside code to freely modify important internal data.
Bad example
Imagine a bank account:
class BankAccount {
double balance;
}
Then anyone can do:
account.balance = -500000;
That's dangerous.
You don't want outside code directly changing the balance.
- Encapsulation using private
Instead:
class BankAccount {
private double balance;
}
Now this won't be allowed from outside:
account.balance = 5000;
because balance is private.
Instead, provide controlled methods:
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
Now:
BankAccount account = new BankAccount();
account.deposit(5000);
System.out.println(account.getBalance());
Output:
5000.0
- Real-time example — Bank ATM
Think about an ATM.
You don't directly access the bank's database and do:
balance = 100000
Instead, you interact through controlled operations:
ATM
│
├── deposit()
├── withdraw()
└── checkBalance()
The actual balance is protected internally.
That's the basic idea of encapsulation.
Outside world
│
▼
┌─────────────────────┐
│ BankAccount │
│ │
│ private balance │
│ │
│ deposit() │
│ withdraw() │
│ getBalance() │
└─────────────────────┘
- Why encapsulation matters in backend development
You'll use this concept constantly when building Java backend applications.
For example:
class User {
private String password;
public void setPassword(String password) {
// validation / hashing
}
public boolean verifyPassword(String password) {
// verification
}
}
You don't want code throughout your application freely manipulating sensitive internal state.
This becomes especially important when you later learn:
Java
↓
Spring Boot
↓
REST APIs
↓
Services
↓
Repositories
↓
Database
Top comments (0)