OOP Fundamentals in Java
Java Master Course — Chapter 11 of 50
This chapter begins the most important section of the Java course: Object-Oriented Programming (OOP).
Java is heavily object-oriented. To become strong in Java, you must understand not only how to write classes and objects, but also why they exist, how they model real-world systems, and how objects communicate with one another.
This chapter focuses on the foundations. Constructors,
this,static, encapsulation, inheritance, polymorphism, abstraction, and interfaces will be studied in later chapters in much greater depth.
1. What Is OOP?#
OOP stands for:
Object-Oriented Programming
It is a programming approach where a program is designed around:
objects
An object represents some entity and combines:
data
+
behavior
For example, a bank account can have:
Data:
account number
owner name
balance
Behavior:
deposit
withdraw
check balance
In Java, we model such entities using classes and objects.
2. Why Do We Need OOP?#
Imagine you are building a college management system.
You may have:
Students
Teachers
Courses
Departments
Exams
Fees
Attendance
If everything is written as unrelated variables and methods, a large program becomes difficult to manage.
OOP gives us a way to group related data and behavior.
For example:
Student
├── name
├── age
├── rollNumber
├── marks
└── study()
Teacher
├── name
├── subject
├── employeeId
└── teach()
Course
├── name
├── code
└── enrollStudent()
This makes the program easier to understand and organize.
3. OOP in Simple Language#
The easiest way to think about OOP is:
Create software objects that represent things or concepts in the problem you are solving.
For example, in a shopping application:
Customer
Product
Cart
Order
Payment
Address
Each object can have:
state
+
behavior
4. What Is an Object?#
An object is an instance of a class.
For example:
Student s1 = new Student();
Here:
Student
is the class.
And:
s1
refers to a Student object.
5. Real-World Object Example#
Think about a real student.
A student has:
Name
Age
Roll number
Course
Marks
These are properties or data.
The student can:
study
attend class
write exam
submit assignment
These are behaviors.
In OOP:
properties → fields
behaviors → methods
6. What Is a Class?#
A class is a blueprint or type definition used to describe objects.
Example:
class Student {
String name;
int age;
void study() {
System.out.println(name + " is studying.");
}
}
The class describes what a Student object can contain and do.
It does not mean that one particular student already exists.
7. Class as a Blueprint#
Think of a house blueprint.
A blueprint defines:
rooms
doors
windows
dimensions
But the blueprint itself is not a physical house.
Similarly:
class Student {
String name;
int age;
}
defines the structure and behavior of Student objects.
8. Class vs Object#
This distinction is extremely important.
Class:
blueprint/type
Object:
actual instance
Example:
class Student {
String name;
int age;
}
Objects:
Student s1 = new Student();
Student s2 = new Student();
Now there are two Student objects.
9. Multiple Objects from One Class#
One class can create many objects.
Example:
class Student {
String name;
int age;
}
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
Conceptually:
Student class
│
┌──────────┼──────────┐
↓ ↓ ↓
s1 s2 s3
Student Student Student
Each object has its own instance state.
10. Creating a Class#
Basic syntax:
class ClassName {
// fields
// methods
}
Example:
class Car {
String brand;
int speed;
void drive() {
System.out.println("Car is driving.");
}
}
11. Creating an Object#
Use:
new
Example:
Car car = new Car();
There are two important parts:
Car
is the reference type.
new Car()
creates a new Car object.
12. Understanding new#
The expression:
new Car()
creates a new instance of Car.
Example:
Car car = new Car();
Conceptually:
car
│
▼
Car object
┌──────────────┐
│ brand │
│ speed │
└──────────────┘
The variable car stores a reference to that object.
13. Accessing Object Fields#
Example:
class Student {
String name;
int age;
}
Create object:
Student s1 = new Student();
Set fields:
s1.name = "Aman";
s1.age = 20;
Read fields:
System.out.println(s1.name);
System.out.println(s1.age);
Output:
Aman
20
14. Dot Operator#
The dot:
.
is commonly used to access members through an object reference.
Example:
s1.name
s1.age
s1.study()
It means, conceptually:
access a member associated with the referenced object/type
15. Adding Methods to a Class#
Example:
class Student {
String name;
int age;
void study() {
System.out.println(name + " is studying.");
}
}
Call:
Student s1 = new Student();
s1.name = "Aman";
s1.study();
Output:
Aman is studying.
16. Object State#
The current values stored in an object's instance fields represent its state.
Example:
Student s1 = new Student();
s1.name = "Aman";
s1.age = 20;
State:
name = Aman
age = 20
Another object can have different state:
Student s2 = new Student();
s2.name = "Riya";
s2.age = 21;
17. Object Behavior#
Behavior is what an object can do.
In Java, behavior is usually represented by methods.
Example:
class BankAccount {
double balance;
void deposit(double amount) {
balance += amount;
}
}
The behavior is:
deposit()
18. State + Behavior#
A useful OOP mental model:
OBJECT
│
┌────────┴────────┐
↓ ↓
STATE BEHAVIOR
│ │
fields methods
Example:
Car
State:
brand
speed
fuel
Behavior:
accelerate()
brake()
refuel()
19. Real-World Modeling#
Suppose the problem is:
Build a library management system.
Possible entities:
Book
Member
Librarian
Library
Loan
Book:
title
author
ISBN
availability
borrow()
returnBook()
Member:
name
memberId
borrowBook()
returnBook()
This is object-oriented modeling.
20. Not Everything Must Be a Physical Object#
OOP does not mean every class must represent a physical object.
Classes can represent concepts such as:
Order
Payment
Transaction
Connection
Configuration
Report
DateRange
Coordinate
They are software models.
21. OOP Is About Modeling Responsibilities#
A good object should have a clear responsibility.
For example:
class BankAccount {
double balance;
void deposit(double amount) {
balance += amount;
}
}
The account is responsible for managing its balance-related behavior.
You do not need to put unrelated logic into the same class.
22. Procedural Programming#
Before understanding OOP, understand procedural programming.
Procedural programming organizes programs mainly around:
functions
+
data
Example:
double balance = 1000;
void deposit(double amount) {
balance += amount;
}
The data and operations can be separate.
23. Procedural Example#
Imagine:
String studentName = "Aman";
int studentMarks = 90;
void printStudent() {
System.out.println(studentName);
System.out.println(studentMarks);
}
As the program grows, you may have many global-like data items and functions that operate on them.
Managing relationships between data and operations becomes harder.
24. OOP Version#
Instead:
class Student {
String name;
int marks;
void printStudent() {
System.out.println(name);
System.out.println(marks);
}
}
Then:
Student student = new Student();
student.name = "Aman";
student.marks = 90;
student.printStudent();
The related data and behavior are grouped inside the class.
25. Procedural vs OOP#
| Procedural | OOP |
|---|---|
| Focus on functions/procedures | Focus on objects |
| Data and operations may be separate | Data and behavior can be grouped |
| Often top-down organization | Often model-driven organization |
| Can become harder to maintain at large scale | Can provide strong structure for large systems |
| Reuse through functions/modules | Reuse through classes, composition, inheritance, etc. |
This does not mean procedural programming is bad.
Both approaches are useful.
26. Java and OOP#
Java strongly supports object-oriented programming.
You work with:
classes
objects
interfaces
inheritance
encapsulation
polymorphism
abstraction
Java also has primitive types:
int
double
char
boolean
so it is not accurate to say that literally everything in Java is an object.
27. Is Everything in Java an Object?#
No.
Java has primitive types.
For example:
int x = 10;
x is an int, a primitive value.
Java also has reference types:
String name = "Java";
Student student = new Student();
So a better statement is:
Java is an object-oriented language with both primitive types and reference types.
28. Reference Types#
Examples:
String
Student
Car
int[]
Student[]
Variables of reference types hold references to objects.
Example:
Student s1 = new Student();
s1 is a reference variable.
29. Primitive Types#
Examples:
int age = 20;
double price = 99.5;
boolean active = true;
char grade = 'A';
These are primitive values.
Wrapper classes such as:
Integer
Double
Boolean
Character
are reference types and will be studied later.
30. Instance Fields#
Consider:
class Student {
String name;
int age;
}
These fields:
name
age
are instance fields.
Every Student object has its own values for these fields.
31. Different Objects, Different State#
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Aman";
s2.name = "Riya";
System.out.println(s1.name);
System.out.println(s2.name);
Output:
Aman
Riya
Changing s1.name does not automatically change s2.name.
32. Object Identity#
Two objects can contain identical data but still be different objects.
Example:
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Aman";
s2.name = "Aman";
They have equal field values, but:
s1
and:
s2
refer to different object instances.
33. Reference Equality#
For objects:
s1 == s2
checks whether both references point to the same object.
Example:
Student s1 = new Student();
Student s2 = new Student();
System.out.println(s1 == s2);
Output:
false
34. Same Object Through Two References#
Student s1 = new Student();
Student s2 = s1;
System.out.println(s1 == s2);
Output:
true
Both references identify the same object.
35. Object Diagram#
Consider:
Student s1 = new Student();
Student s2 = s1;
Conceptually:
s1 ─────┐
│
▼
┌──────────────┐
│ Student │
│ name = null │
│ age = 0 │
└──────────────┘
▲
│
s2 ─────┘
There is one object and two references.
36. Multiple Objects#
Student s1 = new Student();
Student s2 = new Student();
Conceptually:
s1 ───► Student object A
s2 ───► Student object B
There are two separate objects.
37. Object Lifecycle — Basic View#
At a high level:
class definition
↓
object creation
↓
object used
↓
references may disappear
↓
object becomes unreachable
↓
eligible for garbage collection
Garbage collection will be studied in depth later.
38. Default Field Values#
When an object is created, its instance fields receive default values if they are not explicitly initialized.
Example:
class Student {
String name;
int age;
boolean active;
}
New object:
Student s = new Student();
Conceptually:
name → null
age → 0
active → false
39. Field Initializers#
You can provide initial values:
class Student {
String name = "Unknown";
int age = 18;
}
New object:
Student s = new Student();
starts with:
name = Unknown
age = 18
40. Methods Belong to the Class#
Example:
class Student {
String name;
void study() {
System.out.println(name + " is studying.");
}
}
The method is defined once in the class.
Each object can invoke it using its own state:
s1.study();
s2.study();
41. Same Method, Different Result#
class Student {
String name;
void introduce() {
System.out.println("I am " + name);
}
}
Usage:
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Aman";
s2.name = "Riya";
s1.introduce();
s2.introduce();
Output:
I am Aman
I am Riya
The same method operates using the state of the particular object through which it was called.
42. The Hidden this Idea#
Inside an instance method, Java provides the special reference:
this
which refers to the current object.
Example:
class Student {
String name;
void printName() {
System.out.println(this.name);
}
}
If:
s1.printName();
then inside that method:
this → s1
this will be studied deeply in Chapter 14.
43. Instance Method#
A method that normally operates on object state is an instance method.
Example:
class Car {
int speed;
void accelerate() {
speed += 10;
}
}
Call:
Car car = new Car();
car.accelerate();
The method operates on that particular Car object.
44. Static Method Preview#
A static method belongs to the class rather than a particular object.
Example:
class MathUtil {
static int add(int a, int b) {
return a + b;
}
}
Call:
int result = MathUtil.add(10, 20);
You do not need a MathUtil object for this method.
static will be studied in detail in Chapter 14.
45. Object Communication#
One of the important ideas in OOP is that objects can communicate by calling methods on one another.
Example:
class Printer {
void print(String message) {
System.out.println(message);
}
}
class Report {
void generate(Printer printer) {
printer.print("Report generated.");
}
}
Usage:
Printer printer = new Printer();
Report report = new Report();
report.generate(printer);
The Report object uses the Printer object.
46. Objects Working Together#
Real applications usually contain many objects.
For example:
Order
↓
Customer
↓
Address
Order
↓
Payment
Order
↓
Product
Objects collaborate rather than existing in isolation.
This idea becomes central to good OOP design.
47. Modeling a Bank Account#
Let's model:
BankAccount
State:
accountNumber
owner
balance
Behavior:
deposit()
withdraw()
showBalance()
Example:
class BankAccount {
String accountNumber;
String owner;
double balance;
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
balance -= amount;
}
void showBalance() {
System.out.println("Balance = " + balance);
}
}
48. Using the BankAccount Object#
BankAccount account = new BankAccount();
account.accountNumber = "A101";
account.owner = "Aman";
account.balance = 1000;
account.deposit(500);
account.withdraw(200);
account.showBalance();
Output:
Balance = 1300.0
This is only a basic model.
Later, encapsulation will make the design safer.
49. Why This BankAccount Is Not Yet a Good Design#
Currently:
account.balance = -1000000;
is possible.
Anyone can directly change the balance.
That is dangerous.
Later, encapsulation will allow us to protect the internal state:
private double balance;
and control changes through methods.
This is one of the major reasons OOP is useful.
50. Modeling a Car#
class Car {
String brand;
String color;
int speed;
void accelerate() {
speed += 10;
}
void brake() {
if (speed >= 10) {
speed -= 10;
}
}
void showSpeed() {
System.out.println("Speed = " + speed);
}
}
Usage:
Car car = new Car();
car.brand = "Toyota";
car.color = "White";
car.accelerate();
car.accelerate();
car.showSpeed();
Output:
Speed = 20
51. Modeling a Rectangle#
State:
length
width
Behavior:
area()
perimeter()
Example:
class Rectangle {
double length;
double width;
double area() {
return length * width;
}
double perimeter() {
return 2 * (length + width);
}
}
Usage:
Rectangle rectangle = new Rectangle();
rectangle.length = 10;
rectangle.width = 5;
System.out.println(rectangle.area());
System.out.println(rectangle.perimeter());
Output:
50.0
30.0
52. Modeling a Student#
class Student {
String name;
int rollNumber;
int marks;
void study() {
System.out.println(name + " is studying.");
}
void showResult() {
System.out.println(
name + " scored " + marks
);
}
}
Usage:
Student student = new Student();
student.name = "Aman";
student.rollNumber = 101;
student.marks = 88;
student.study();
student.showResult();
53. Real-World Modeling Is About Choosing the Right Boundaries#
Suppose an online store has:
Customer
Order
Product
Payment
You should ask:
What data belongs to each concept?
What behavior belongs to each concept?
Which object should be responsible for each operation?
Which objects need to communicate?
These questions are more important than simply creating many classes.
54. Bad OOP Design — One Giant Class#
A beginner may create:
class Everything {
// student logic
// payment logic
// database logic
// email logic
// order logic
// product logic
}
This becomes difficult to maintain.
A better design separates responsibilities.
For example:
StudentService
PaymentService
Order
Product
EmailService
The exact design depends on the application.
55. Class Responsibility#
A class should have a clear purpose.
For example:
class Invoice {
// invoice-related data and behavior
}
instead of:
class Invoice {
// invoice
// database
// email
// authentication
// UI
}
This connects to cohesion and SOLID principles, which will be studied later.
56. Abstraction Preview#
OOP lets us hide unnecessary implementation details.
For example:
account.withdraw(500);
The caller does not need to know every internal step.
The method may internally:
check balance
validate amount
update balance
record transaction
The caller only needs the appropriate public operation.
This idea is called:
abstraction
and will be studied deeply in Chapter 20.
57. Encapsulation Preview#
Encapsulation means controlling access to an object's internal state and behavior.
Instead of:
account.balance = -1000;
we can design:
private double balance;
public void withdraw(double amount) {
// validation
}
This helps protect the object's rules.
Chapter 15 covers encapsulation in detail.
58. Inheritance Preview#
Inheritance lets one class derive from another.
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
Now Dog inherits accessible members from Animal according to Java's inheritance/access rules.
Inheritance will be studied in Chapter 16.
59. Polymorphism Preview#
Polymorphism means that one common interface or parent type can work with different object types.
Example:
Animal animal = new Dog();
If Dog overrides an instance method:
animal.sound();
can invoke the Dog implementation at runtime.
This will be studied deeply in Chapters 18 and 19.
60. Abstraction, Encapsulation, Inheritance, Polymorphism#
These are commonly called the four major OOP pillars:
Encapsulation
Inheritance
Polymorphism
Abstraction
But remember:
OOP is larger than just memorizing four words.
Good OOP also involves:
composition
interfaces
responsibilities
coupling
cohesion
object collaboration
design principles
61. The Four Pillars — Simple Meaning#
Encapsulation:
Protect and control object state.
Inheritance:
Create a subtype relationship using an existing class.
Polymorphism:
One common type can work with different implementations.
Abstraction:
Expose essential behavior while hiding unnecessary implementation details.
These will be covered one by one.
62. OOP Is Not Just Classes#
Beginners sometimes think:
OOP = create classes
That is incomplete.
You can create hundreds of classes and still have terrible OOP design.
Good OOP asks:
Who owns this data?
Who should perform this operation?
What should be public?
What should be hidden?
Which objects collaborate?
Should we use inheritance or composition?
63. Class as a New Type#
When you define:
class Student {
String name;
int marks;
}
you are defining a new reference type:
Student
Now Java understands variables such as:
Student s;
just as it understands:
String text;
64. User-Defined Types#
Classes allow you to create types that match your problem domain.
For example:
class Product {
String name;
double price;
}
Now:
Product product;
is a meaningful type in the application.
65. Object State Can Change#
Objects are often mutable.
Example:
class Counter {
int value;
void increment() {
value++;
}
}
Usage:
Counter counter = new Counter();
counter.increment();
counter.increment();
System.out.println(counter.value);
Output:
2
The object's state changed over time.
66. Immutable Objects Preview#
Not all objects should be mutable.
An immutable object is designed so its state cannot be changed after creation.
String is an important example.
You can design immutable classes later using techniques such as:
private final fields
constructor initialization
no setters
defensive copies when needed
Immutability and good OOP design will be studied further in Chapter 23.
67. Object Identity vs State#
Two objects can have:
same state
but different identities.
Example:
Student A:
name = Aman
marks = 90
Student B:
name = Aman
marks = 90
They may represent two separate students despite identical field values.
This distinction is important when working with objects.
68. Object Equality#
Do not automatically assume:
a == b
means objects are logically equal.
For ordinary objects:
==
checks reference identity.
Logical equality can be defined using:
equals()
when the class implements it appropriately.
equals() and hashCode() will be discussed later.
69. Object References Can Be null#
Example:
Student student = null;
The variable currently refers to no object.
Calling:
student.study();
causes:
NullPointerException
because there is no Student object to receive the method call.
70. Null Reference Diagram#
student
│
▼
null
There is no object at the end of the reference.
71. Creating an Object After null#
Student student = null;
student = new Student();
student.name = "Aman";
Now:
student ───► Student object
72. Losing a Reference#
Example:
Student student = new Student();
student = new Student();
The first Student object may become unreachable if no other reference points to it.
It can eventually become eligible for garbage collection.
73. Multiple References#
Student a = new Student();
Student b = a;
Student c = b;
All three references point to the same object.
a ─────┐
b ─────┼──► Student object
c ─────┘
Changing the object through one reference is visible through the others.
74. Example of Shared Reference#
Student a = new Student();
Student b = a;
a.name = "Aman";
System.out.println(b.name);
Output:
Aman
Because a and b refer to the same object.
75. Assigning a New Object#
Student a = new Student();
Student b = a;
a = new Student();
Now:
a ───► Student object B
b ───► Student object A
Changing a to refer to another object does not change b.
76. Objects Are Passed by Value#
Java always passes arguments by value.
For an object parameter, the value being copied is the reference.
Example:
static void changeName(Student student) {
student.name = "Changed";
}
Call:
Student s = new Student();
s.name = "Original";
changeName(s);
System.out.println(s.name);
Output:
Changed
The method received a copy of the reference that points to the same object.
77. Reassigning the Parameter#
static void replace(Student student) {
student = new Student();
student.name = "New";
}
Caller:
Student s = new Student();
s.name = "Original";
replace(s);
System.out.println(s.name);
Output:
Original
Why?
The parameter was reassigned locally.
The caller's reference was not changed.
78. Class Fields vs Local Variables#
Example:
class Student {
String name;
int age;
void show() {
int marks = 90;
}
}
Here:
name
age
are fields.
marks
is a local variable.
Fields receive default values.
Local variables must be definitely assigned before use.
79. Method Parameters#
Example:
void setName(String name) {
...
}
The parameter:
name
is local to the method invocation.
It is not automatically an object field.
80. Naming Conflict#
Consider:
class Student {
String name;
void setName(String name) {
name = name;
}
}
This does not update the field.
Both name references in the assignment refer to the parameter.
Correct:
class Student {
String name;
void setName(String name) {
this.name = name;
}
}
this.name refers to the field of the current object.
This is why this is so important.
81. this Preview#
Inside:
class Student {
String name;
void print() {
System.out.println(this.name);
}
}
this refers to the current Student object.
If:
Student s1 = new Student();
s1.print();
then inside print():
this → s1
82. Class Members#
A class can contain many kinds of members:
fields
methods
constructors
nested types
Example:
class Student {
String name;
Student() {
// constructor
}
void study() {
// method
}
}
Constructors will be covered in Chapter 13.
83. Access Modifiers Preview#
Java provides access control such as:
public
private
protected
package-private
Example:
class Student {
private int marks;
}
private means the field is directly accessible only within the appropriate class.
Access modifiers and packages will be covered later.
84. Why private Matters#
Without access control:
account.balance = -999999;
could violate business rules.
With:
private double balance;
you can force callers to use controlled methods.
This leads to encapsulation.
85. Object-Oriented Thinking#
When given a programming problem, ask:
What entities exist?
What information does each entity have?
What behavior does each entity need?
Which object should own each behavior?
How do objects communicate?
Which state should be protected?
This is the beginning of OOP design.
86. Example — Online Shopping#
Problem:
Build an online shopping system.
Possible classes:
Customer
Product
Cart
Order
Payment
Address
Customer:
name
email
address
Product:
name
price
stock
Cart:
items
addProduct()
removeProduct()
calculateTotal()
Order:
orderId
items
status
placeOrder()
cancelOrder()
Payment:
amount
status
pay()
The exact architecture depends on requirements.
87. Example — College System#
Possible classes:
Student
Teacher
Course
Department
Exam
Result
Student:
name
rollNumber
courses
Teacher:
name
employeeId
courses
Course:
code
name
credits
Result:
student
course
marks
grade
This is domain modeling.
88. Example — Banking System#
Possible classes:
Customer
BankAccount
Transaction
Bank
Loan
BankAccount:
accountNumber
balance
owner
deposit()
withdraw()
Transaction:
amount
type
timestamp
Customer:
name
customerId
accounts
89. Example — Game#
Possible classes:
Player
Enemy
Weapon
Game
Level
Inventory
Player:
health
score
position
Behavior:
move()
attack()
takeDamage()
Enemy:
health
position
Behavior:
attack()
move()
This naturally leads toward inheritance and polymorphism.
90. Example — Ride Booking App#
Possible classes:
Rider
Driver
Vehicle
Ride
Payment
Location
Ride:
pickup
destination
fare
status
Behavior:
request()
cancel()
complete()
calculateFare()
Objects communicate:
Rider → Ride
Ride → Driver
Ride → Payment
Ride → Location
91. OOP and Modularity#
A good class can act as a module with a focused responsibility.
For example:
class Invoice {
...
}
can encapsulate invoice-related behavior.
Other code can interact through a clear API.
This reduces the amount of knowledge each part of the program needs about other parts.
92. API of a Class#
The public methods and accessible members of a class form part of its API.
Example:
class BankAccount {
public void deposit(double amount) {
...
}
public void withdraw(double amount) {
...
}
}
A caller uses:
account.deposit(500);
account.withdraw(200);
without needing to know every internal implementation detail.
93. Implementation vs Interface#
Suppose:
account.withdraw(500);
The caller cares about:
withdraw money
The internal implementation might contain:
validation
balance calculation
transaction creation
logging
notifications
This separation between what an object offers and how it implements it is fundamental to abstraction and encapsulation.
94. Good Object Design#
A good object often has:
clear responsibility
valid state
controlled access
cohesive behavior
simple public API
minimal unnecessary dependencies
You will study these ideas in much more detail later.
95. Cohesion Preview#
Cohesion describes how closely related the responsibilities inside a module/class are.
High cohesion:
Student class
→ student-related state and behavior
Low cohesion:
Student class
→ student + database + email + payment + logging + UI
High cohesion is generally desirable.
96. Coupling Preview#
Coupling describes how strongly one class depends on other classes.
High coupling:
A depends heavily on B
B depends heavily on C
C depends heavily on A
This can make changes difficult.
Lower, well-managed coupling is generally desirable.
Chapter 23 will cover coupling and cohesion in detail.
97. Composition Preview#
Instead of using inheritance for everything, objects can contain other objects.
Example:
class Car {
Engine engine;
}
This represents:
Car HAS-A Engine
Composition and other OOP relationships will be covered in Chapter 22.
98. IS-A vs HAS-A Preview#
Inheritance often represents:
IS-A
Example:
Dog IS-A Animal
Composition represents:
HAS-A
Example:
Car HAS-A Engine
Choosing the right relationship is important.
99. Why Composition Is Important#
Beginners often try:
inheritance everywhere
But many relationships are better modeled with composition.
For example:
class Car {
Engine engine;
}
is often more natural than trying to make:
Car extends Engine
because a car is not an engine.
100. Object Collaboration Example#
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
Engine engine = new Engine();
void start() {
engine.start();
System.out.println("Car started");
}
}
Usage:
Car car = new Car();
car.start();
Output:
Engine started
Car started
This is a simple example of composition.
101. OOP Does Not Automatically Mean Better Code#
OOP is a tool.
Bad OOP can create:
too many classes
unnecessary inheritance
deep hierarchies
complex dependencies
boilerplate
Good OOP uses objects where they improve structure and maintainability.
102. Common Beginner Mistake — Class = Object#
Wrong:
class and object are the same
Correct:
class → defines a type
object → instance of that type
103. Common Beginner Mistake — One Object per Class#
A class can create:
zero objects
one object
many objects
There is no rule that one class means one object.
104. Common Beginner Mistake — new Creates the Variable#
Consider:
Student s = new Student();
The variable:
s
is declared as a reference variable.
The:
new Student()
expression creates the object.
105. Common Beginner Mistake — Reference Is Object#
This:
Student s;
does not create a Student object.
It declares a reference variable.
Object creation:
s = new Student();
106. Common Beginner Mistake — Fields Are Local Variables#
Example:
class Student {
int age;
}
age is a field.
It receives a default value when the object is initialized.
But:
void test() {
int age;
}
is a local variable.
You cannot read it before definite assignment.
107. Common Beginner Mistake — Comparing Objects with ==#
Wrong when you want logical content equality:
if (student1 == student2) {
}
This checks whether they are the same object.
For logical equality, a class may define:
equals()
appropriately.
108. Common Beginner Mistake — Making Everything static#
Beginners sometimes write:
static String name;
static int age;
for every field.
This changes the meaning: those fields become class-level shared state rather than per-object state.
If each student needs a different name:
String name;
should normally be an instance field.
109. Instance State vs Shared State#
Instance field:
class Student {
String name;
}
Each object has its own name.
Static field:
class Student {
static int count;
}
The field belongs to the class and is shared across Student instances.
Static will be covered deeply in Chapter 14.
110. Example of Shared State#
class Student {
static int count = 0;
Student() {
count++;
}
}
Usage:
new Student();
new Student();
new Student();
System.out.println(Student.count);
Output:
3
This is a simple example of class-level state.
111. OOP and Reusability#
A class can be reused in many places.
Example:
class Rectangle {
double length;
double width;
double area() {
return length * width;
}
}
Now many parts of a program can create:
Rectangle
objects.
112. OOP and Maintainability#
Suppose the rule for calculating an order total changes.
If the calculation is centralized in an appropriate class:
order.calculateTotal();
you have one clear place to update.
If the same calculation is duplicated in ten different functions, changes become harder and errors become more likely.
Good OOP can reduce such duplication.
113. OOP and Encapsulation#
A class can keep implementation details private.
Example:
class Counter {
private int value;
public void increment() {
value++;
}
public int getValue() {
return value;
}
}
Caller:
Counter counter = new Counter();
counter.increment();
System.out.println(counter.getValue());
The caller does not directly manipulate value.
114. Why Encapsulation Improves Safety#
Suppose:
private double balance;
Then you can enforce:
amount > 0
balance sufficient
transaction rules
inside methods.
This prevents arbitrary external code from directly changing the field.
115. OOP and Abstraction#
A class can expose a small API.
Example:
printer.print(document);
The caller does not need to understand:
buffer management
device communication
encoding
driver details
This is abstraction.
116. OOP and Polymorphism#
Suppose:
interface Payment {
void pay();
}
Different classes:
CardPayment
UPIPayment
CashPayment
can implement the same operation.
Then application code can work with:
Payment
rather than hard-coding every implementation.
Interfaces will be covered in Chapter 21.
117. OOP and Inheritance#
Inheritance can express a subtype relationship.
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
A Dog is an Animal.
But inheritance should represent a genuine subtype relationship, not merely code reuse.
118. Code Reuse Is Not the Only Purpose of Inheritance#
A common beginner explanation is:
inheritance = code reuse
That is incomplete.
Inheritance primarily establishes a type relationship.
It can provide inherited behavior and support polymorphism.
If you only want to reuse implementation, composition may often be better.
119. OOP Pillars in One Example#
Consider:
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
}
Encapsulation:
balance is private
Abstraction:
caller uses deposit()
Inheritance and polymorphism could later allow specialized account types.
This shows how the ideas work together.
120. Class Design Exercise#
Suppose you need a:
Library Book
Ask:
What state does it have?
What behavior does it have?
What should be public?
What should be private?
What other objects does it interact with?
Possible state:
title
author
ISBN
available
Possible behavior:
borrow()
returnBook()
121. Class Design Exercise — Bank Account#
State:
accountNumber
owner
balance
Behavior:
deposit()
withdraw()
transfer()
Questions:
Should balance be public?
Should a caller directly set balance?
Who validates withdrawal?
Who records a transaction?
These questions are the beginning of real OOP design.
122. Class Design Exercise — Shopping Cart#
State:
items
Behavior:
addItem()
removeItem()
calculateTotal()
clear()
Questions:
Should the cart expose its internal collection directly?
Should product prices be copied?
Who calculates discounts?
These become advanced design questions later.
123. Object Responsibility Example#
Suppose:
Order order;
and:
PaymentService paymentService;
A possible design:
paymentService.pay(order);
or:
order.pay(paymentService);
Which is better depends on the domain and architecture.
OOP is not about one universal syntax pattern.
It is about assigning responsibilities sensibly.
124. Encapsulation Does Not Mean "Just Getters and Setters"#
A common beginner definition is:
encapsulation = private variables + getters/setters
That is too narrow.
Good encapsulation means controlling how state is represented and changed.
Sometimes exposing a setter for every field actually weakens the design.
For example:
account.setBalance(-5000);
may be a bad API.
Instead:
account.withdraw(500);
can enforce business rules.
125. OOP and Invariants#
An invariant is a rule that should remain true for an object's valid state.
Example:
Bank account balance cannot be negative
A good class design protects such invariants.
For example:
private double balance;
and controlled methods can prevent invalid changes.
126. Object Construction Preview#
When an object is created:
Student student = new Student();
Java initializes the object and runs an appropriate constructor.
Constructors will be covered in Chapter 13.
127. Why Constructors Matter#
A constructor lets you create objects in a valid initial state.
Instead of:
Student s = new Student();
s.name = "Aman";
s.age = 20;
you can later design:
Student s = new Student("Aman", 20);
This reduces the chance of forgetting required initialization.
128. OOP and Validation#
Suppose:
age must be >= 0
marks must be 0..100
price must not be negative
Good object design can place validation close to the data it protects.
This avoids spreading the same validation rules across many callers.
129. Example — Marks Validation Preview#
class Student {
private int marks;
void setMarks(int marks) {
if (marks < 0 || marks > 100) {
throw new IllegalArgumentException(
"Marks must be between 0 and 100"
);
}
this.marks = marks;
}
}
This is a preview of encapsulation.
130. OOP and State Transitions#
Objects often move between valid states.
Example:
Order
CREATED
↓
PAID
↓
SHIPPED
↓
DELIVERED
The methods of the Order class can control allowed transitions.
This is more meaningful than simply exposing:
order.status = "anything";
131. OOP and Domain Rules#
A strong domain class can represent business rules.
Example:
class Order {
void cancel() {
// check whether cancellation is allowed
// change state
}
}
The caller says:
order.cancel();
rather than manipulating internal fields directly.
132. OOP and Testing#
Well-designed classes are often easier to test because responsibilities are separated.
For example:
Rectangle rectangle = new Rectangle(10, 5);
assert rectangle.area() == 50;
A focused class is easier to reason about than a huge class doing unrelated work.
133. OOP and Reusability Example#
A reusable:
Money
or:
DateRange
class can be used across many parts of an application.
Good domain models can make code more expressive.
Instead of:
double start;
double end;
you can have:
DateRange range;
This can make intent clearer.
134. Object-Oriented Code Reads Like the Domain#
Compare:
calculateTotal(items, discounts, tax, shipping);
with a suitable domain API:
order.calculateTotal();
The second can express intent more clearly if the Order object is truly responsible for the calculation.
Good OOP often improves the language of the code.
135. OOP Does Not Eliminate Functions#
Methods are functions associated with classes.
Java programs still rely heavily on:
methods
loops
conditions
arrays
Strings
OOP builds a structure around these programming fundamentals.
136. OOP and Static Utility Code#
Not every operation requires an object.
For example:
Math.max(10, 20);
is a class-level utility operation.
Good Java design uses both:
instance methods
static methods
when appropriate.
137. Class-Level vs Object-Level Thinking#
Ask:
Does this behavior depend on one object's state?
If yes, it may be an instance method.
Ask:
Does this behavior belong to the type as a whole and need no instance state?
A static method may be appropriate.
This is only a guideline; API design matters.
138. Object-Oriented Vocabulary#
You should know these words:
Class
Object
Instance
Field
Method
State
Behavior
Reference
Instance member
Static member
Constructor
Encapsulation
Inheritance
Polymorphism
Abstraction
Interface
Composition
Association
Later chapters will define each in depth.
139. Class#
Definition:
A class is a Java type that defines members such as fields, methods, constructors, and nested types and is used to create objects.
Example:
class Car {
String brand;
}
140. Object#
Definition:
An object is an instance of a class or another reference type that has identity and state/behavior appropriate to its type.
Example:
Car car = new Car();
141. Instance#
An instance is a particular object of a type.
Example:
Car car = new Car();
car refers to one instance of Car.
142. Field#
A field is a variable declared as a member of a class or interface.
Example:
class Student {
String name;
int age;
}
Here:
name
age
are fields.
143. Method#
A method is a named block of executable behavior declared in a class, interface, enum, or record.
Example:
void study() {
System.out.println("Studying");
}
144. State#
State is the information describing the current condition of an object.
Example:
Student:
name = Aman
age = 20
marks = 90
145. Behavior#
Behavior is what an object can do.
Example:
study()
takeExam()
showResult()
146. Identity#
Object identity means that two separate object instances can be distinguished even if their state happens to be equal.
Example:
Student a = new Student();
Student b = new Student();
a and b identify different objects.
147. Reference#
A reference is a value that can refer to an object.
Example:
Student student = new Student();
The variable:
student
contains a reference value.
148. Instance Member#
A member associated with an object instance.
Example:
class Student {
String name;
void study() {
}
}
name and study() are instance members unless declared otherwise.
149. Static Member#
A member declared with:
static
belongs to the class rather than a particular object instance.
Example:
class Student {
static int count;
}
Detailed treatment comes in Chapter 14.
150. Real-World Modeling Checklist#
When designing a class, ask:
1. What is the entity/concept?
2. What data does it own?
3. What behavior does it own?
4. What rules must always be true?
5. Which fields should be hidden?
6. Which operations should be public?
7. Which objects does it collaborate with?
8. Is inheritance really needed?
9. Would composition be better?
10. Can the class have one clear responsibility?
This checklist is much more valuable than memorizing definitions.
151. Practical Program — Student Class#
class Student {
String name;
int age;
int marks;
void introduce() {
System.out.println(
"Name: " + name +
", Age: " + age
);
}
void showMarks() {
System.out.println(
name + " scored " + marks
);
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
student.name = "Aman";
student.age = 20;
student.marks = 90;
student.introduce();
student.showMarks();
}
}
Output:
Name: Aman, Age: 20
Aman scored 90
152. Practical Program — Multiple Students#
class Student {
String name;
int marks;
void showResult() {
System.out.println(
name + " = " + marks
);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
s1.name = "Aman";
s1.marks = 90;
s2.name = "Riya";
s2.marks = 95;
s3.name = "Raj";
s3.marks = 82;
s1.showResult();
s2.showResult();
s3.showResult();
}
}
Output:
Aman = 90
Riya = 95
Raj = 82
153. Practical Program — Bank Account#
class BankAccount {
String owner;
double balance;
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance");
}
}
void showBalance() {
System.out.println(
owner + "'s balance = " + balance
);
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.owner = "Aman";
account.balance = 1000;
account.deposit(500);
account.withdraw(300);
account.showBalance();
}
}
Output:
Aman's balance = 1200.0
This is intentionally a basic version. Encapsulation will improve it later.
154. Practical Program — Rectangle#
class Rectangle {
double length;
double width;
double area() {
return length * width;
}
double perimeter() {
return 2 * (length + width);
}
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.length = 10;
rectangle.width = 5;
System.out.println(
"Area = " + rectangle.area()
);
System.out.println(
"Perimeter = " + rectangle.perimeter()
);
}
}
Output:
Area = 50.0
Perimeter = 30.0
155. Practical Program — Car#
class Car {
String brand;
int speed;
void accelerate() {
speed += 10;
}
void brake() {
if (speed >= 10) {
speed -= 10;
}
}
void show() {
System.out.println(
brand + " speed = " + speed
);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.brand = "Toyota";
car.accelerate();
car.accelerate();
car.brake();
car.show();
}
}
Output:
Toyota speed = 10
156. Practical Program — Counter#
class Counter {
int value;
void increment() {
value++;
}
void decrement() {
value--;
}
void show() {
System.out.println("Value = " + value);
}
}
public class Main {
public static void main(String[] args) {
Counter counter = new Counter();
counter.increment();
counter.increment();
counter.decrement();
counter.show();
}
}
Output:
Value = 1
157. Practical Program — Object Communication#
class Printer {
void print(String message) {
System.out.println(message);
}
}
class Report {
void generate(Printer printer) {
printer.print("Report generated.");
}
}
public class Main {
public static void main(String[] args) {
Printer printer = new Printer();
Report report = new Report();
report.generate(printer);
}
}
Output:
Report generated.
The Report object collaborates with the Printer object.
158. Practical Program — Composition#
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
private final Engine engine = new Engine();
void start() {
engine.start();
System.out.println("Car started");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Output:
Engine started
Car started
This demonstrates a simple HAS-A relationship.
159. Practical Program — Object Reference#
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student a = new Student();
Student b = a;
a.name = "Aman";
System.out.println(b.name);
}
}
Output:
Aman
Both references point to the same object.
160. Practical Program — Separate Objects#
class Student {
String name;
}
public class Main {
public static void main(String[] args) {
Student a = new Student();
Student b = new Student();
a.name = "Aman";
b.name = "Riya";
System.out.println(a.name);
System.out.println(b.name);
}
}
Output:
Aman
Riya
They are separate objects.
161. Practical Program — Object as Method Argument#
class Student {
String name;
}
public class Main {
static void changeName(Student student) {
student.name = "Changed";
}
public static void main(String[] args) {
Student student = new Student();
student.name = "Original";
changeName(student);
System.out.println(student.name);
}
}
Output:
Changed
The method received a copied reference value pointing to the same object.
162. Practice — Basic Class Creation#
Create a:
Book
class with:
title
author
price
and methods:
display()
applyDiscount()
Create at least two Book objects.
163. Practice — Mobile Phone#
Create:
MobilePhone
with:
brand
model
price
battery
Methods:
call()
charge()
showDetails()
Create two different objects.
164. Practice — Bank Account#
Create:
BankAccount
with:
owner
accountNumber
balance
Methods:
deposit()
withdraw()
showBalance()
Try to maintain valid balance rules.
You will improve this class using encapsulation in Chapter 15.
165. Practice — Employee#
Create:
Employee
with:
name
employeeId
salary
department
Methods:
work()
showDetails()
calculateAnnualSalary()
Create three employees.
166. Practice — Rectangle#
Create:
Rectangle
with:
length
width
Methods:
area()
perimeter()
isSquare()
167. Practice — Circle#
Create:
Circle
with:
radius
Methods:
area()
circumference()
diameter()
Use:
Math.PI
168. Practice — Product#
Create:
Product
with:
name
price
quantity
Methods:
totalPrice()
display()
Create several Product objects.
169. Practice — Library Book#
Create:
Book
with:
title
author
available
Methods:
borrow()
returnBook()
showStatus()
Try to prevent borrowing an unavailable book.
170. Practice — Movie#
Create:
Movie
with:
title
rating
duration
Methods:
showDetails()
isHit()
Define your own rule for isHit().
171. Practice — Temperature#
Create:
Temperature
with:
celsius
Methods:
toFahrenheit()
toKelvin()
172. Practice — Simple Counter#
Create:
Counter
with:
value
Methods:
increment()
decrement()
reset()
show()
Then create multiple Counter objects and observe that each has separate state.
173. Interview Questions#
Q1. What is OOP?#
OOP is a programming paradigm that organizes software around objects and types, combining data and behavior and supporting concepts such as encapsulation, inheritance, polymorphism, and abstraction.
Q2. What is a class?#
A class is a Java type definition that describes the members and behavior of its instances.
Q3. What is an object?#
An object is an instance of a class or another reference type.
Q4. Difference between class and object?#
Class → type/definition
Object → actual instance
Q5. Can one class have multiple objects?#
Yes.
A class can be instantiated many times.
Q6. Is everything in Java an object?#
No.
Java has both primitive types and reference types.
Q7. What is state?#
The current data/condition of an object.
Q8. What is behavior?#
Operations an object can perform, usually represented by methods.
Q9. What is object identity?#
The identity that distinguishes one object instance from another.
Q10. What does new do?#
It creates a new object/array instance and returns a reference to it.
Q11. What is a reference variable?#
A variable whose value can refer to an object.
Q12. What happens with:#
Student s;
No Student object is created by that declaration.
It only declares a reference variable.
Q13. What happens with:#
Student s = new Student();
A Student object is created and the reference returned by new Student() is assigned to s.
Q14. What does == do for object references?#
It checks whether two references identify the same object.
Q15. How do two objects have the same state but different identity?#
They can be separate instances with equal field values.
Q16. What is encapsulation?#
Encapsulation is the design practice of controlling access to an object's internal state and exposing appropriate operations.
Q17. What is inheritance?#
Inheritance allows a class to derive from another class and establishes a subtype relationship.
Q18. What is polymorphism?#
Polymorphism allows code to work through a common type while the actual object can provide different implementations, especially through overriding and interfaces.
Q19. What is abstraction?#
Abstraction focuses on exposing essential operations while hiding unnecessary implementation details.
Q20. What are the four commonly taught OOP pillars?#
Encapsulation
Inheritance
Polymorphism
Abstraction
174. Interview Question — Is OOP the Same as Classes?#
No.
Classes are a mechanism for defining types.
OOP also involves:
object collaboration
encapsulation
abstraction
polymorphism
inheritance
composition
responsibility
design
175. Interview Question — Is Java Purely Object-Oriented?#
No, not in the strict sense.
Java includes primitive types such as:
int
double
boolean
char
alongside reference types and object-oriented features.
176. Interview Question — What Is an Instance?#
A particular object created from a class/type.
Example:
Student s = new Student();
s refers to an instance of Student.
177. Interview Question — What Is the Difference Between Object and Reference?#
An object is the actual runtime entity.
A reference is a value that can identify/refer to that object.
Example:
Student s = new Student();
Conceptually:
s → reference
new ... → object
178. Interview Question — Can Two References Point to One Object?#
Yes.
Student a = new Student();
Student b = a;
Both point to the same object.
179. Interview Question — Can One Reference Point to Different Objects Over Time?#
Yes.
Student s = new Student();
s = new Student();
The variable now refers to the second object.
180. Interview Question — What Is null?#
null is a special reference value that means a reference currently does not identify an object.
It is not an object.
181. Interview Question — What Happens If You Call a Method on Null?#
Example:
Student s = null;
s.study();
This results in:
NullPointerException
182. Interview Question — What Is Encapsulation Beyond Getters and Setters?#
Encapsulation is about controlling representation and access and protecting object invariants.
It is not simply a requirement to generate a getter and setter for every field.
183. Interview Question — Why Is Composition Important?#
Composition lets objects contain or collaborate with other objects.
It often models:
HAS-A
relationships and can avoid unnecessary inheritance hierarchies.
184. Interview Question — Why Should Inheritance Not Be Used Everywhere?#
Inheritance establishes a subtype relationship and introduces coupling between parent and child types.
If there is no genuine subtype relationship, composition is often more appropriate.
185. Interview Question — What Is High Cohesion?#
A class has high cohesion when its responsibilities are strongly related and focused.
186. Interview Question — What Is Coupling?#
Coupling describes the degree of dependency between components.
Generally, lower and well-managed coupling makes systems easier to change.
187. Output Questions#
Question 1#
class Student {
String name;
}
Student s = new Student();
System.out.println(s.name);
Output:
null
Question 2#
class Student {
int age;
}
Student s = new Student();
System.out.println(s.age);
Output:
0
Question 3#
class Student {
String name;
}
Student a = new Student();
Student b = new Student();
a.name = "Aman";
b.name = "Riya";
System.out.println(a.name);
System.out.println(b.name);
Output:
Aman
Riya
Question 4#
class Student {
String name;
}
Student a = new Student();
Student b = a;
a.name = "Aman";
System.out.println(b.name);
Output:
Aman
Question 5#
class Counter {
int value;
void increment() {
value++;
}
}
Counter a = new Counter();
Counter b = new Counter();
a.increment();
a.increment();
b.increment();
System.out.println(a.value);
System.out.println(b.value);
Output:
2
1
Each object has separate instance state.
Question 6#
class Student {
}
Student a = new Student();
Student b = new Student();
System.out.println(a == b);
Output:
false
Question 7#
class Student {
}
Student a = new Student();
Student b = a;
System.out.println(a == b);
Output:
true
Question 8#
class Student {
String name;
}
static void change(Student s) {
s.name = "Changed";
}
If:
Student student = new Student();
student.name = "Original";
change(student);
System.out.println(student.name);
Output:
Changed
Question 9#
class Student {
String name;
}
static void replace(Student s) {
s = new Student();
s.name = "New";
}
If:
Student student = new Student();
student.name = "Original";
replace(student);
System.out.println(student.name);
Output:
Original
Question 10#
class Student {
String name = "Unknown";
}
Then:
Student s = new Student();
System.out.println(s.name);
Output:
Unknown
The field initializer provides the initial value.
188. Conceptual Questions to Test Yourself#
Answer these without looking back:
1. What is the difference between class and object?
2. What is the difference between object and reference?
3. What is object state?
4. What is object behavior?
5. Why do we use classes?
6. Why can one class create many objects?
7. What does new do?
8. What happens when a reference is assigned to another reference?
9. Why does == not normally compare object content?
10. Why is encapsulation useful?
11. What is composition?
12. What does IS-A mean?
13. What does HAS-A mean?
14. Why is inheritance not simply code reuse?
15. Why is good OOP about responsibilities?
189. Design Exercise — Identify Objects#
For an:
Online Food Delivery App
identify at least:
5 classes
Possible answers:
Customer
Restaurant
FoodItem
Order
DeliveryPartner
Payment
Address
Then identify:
state
behavior
relationships
for each.
190. Design Exercise — Identify State and Behavior#
For:
Car
identify:
State:
?
Behavior:
?
Possible answer:
State:
brand
speed
fuel
Behavior:
accelerate
brake
refuel
191. Design Exercise — Find Bad Responsibility#
Consider:
class Student {
String name;
void calculateTax() {
}
void sendEmail() {
}
void saveToDatabase() {
}
void study() {
}
}
Question:
Is this good class design?
Probably not.
Why?
Because the class has unrelated responsibilities.
A better design may separate:
Student
TaxService
EmailService
StudentRepository
The exact architecture depends on the application.
192. Design Exercise — Composition or Inheritance?#
Decide whether each relationship is more naturally:
IS-A
or:
HAS-A
Examples:
Dog / Animal
Car / Engine
Student / Address
Manager / Employee
House / Room
Laptop / Battery
Possible answers:
Dog IS-A Animal
Manager IS-A Employee
Car HAS-A Engine
Student HAS-A Address
House HAS-A Room
Laptop HAS-A Battery
The exact modeling can depend on the domain.
193. Design Exercise — Build a Simple Library#
Create:
Book
Member
Library
Book:
title
author
available
Member:
name
memberId
Library:
books
members
Operations:
addBook()
registerMember()
borrowBook()
returnBook()
Do not worry about advanced collections yet.
You can initially use arrays if necessary.
194. Mini Project — Student Management#
Build a small program with:
Student
Fields:
name
rollNumber
marks
Methods:
showDetails()
calculateGrade()
isPassed()
Create at least five Student objects.
Later, improve this project using:
constructors
encapsulation
arrays/collections
inheritance
interfaces
195. Mini Project — Bank Account System#
Create:
BankAccount
Support:
deposit
withdraw
balance display
Create multiple accounts.
Then add:
account number
transaction history
transfer
In later chapters, improve it with encapsulation and OOP relationships.
196. Mini Project — Library System#
Create:
Book
Member
Library
Support:
add book
show books
borrow book
return book
Use objects to represent each entity.
197. Mini Project — Shopping Cart#
Create:
Product
Cart
Product:
name
price
Cart:
products
addProduct()
removeProduct()
calculateTotal()
Later you can add:
discount
tax
payment
order
198. Mini Project — Simple Game#
Create:
Player
Enemy
Player:
name
health
score
Methods:
attack()
takeDamage()
Enemy:
name
health
damage
Methods:
attack()
This project will become much more interesting after inheritance and polymorphism.
199. OOP Learning Path#
You are now at:
Chapter 11
OOP Fundamentals
Next:
Chapter 12
Classes & Objects
Then:
Chapter 13
Constructors
Then:
Chapter 14
this & static
Then:
Chapter 15
Encapsulation
Then:
Chapter 16
Inheritance
Then:
Chapter 17
Method Overloading
Then:
Chapter 18
Method Overriding
Then:
Chapter 19
Polymorphism
Then:
Chapter 20
Abstraction
Then:
Chapter 21
Interfaces
Then:
Chapter 22
OOP Relationships
Then:
Chapter 23
OOP Design
This sequence is intentionally designed to build the concepts step by step.
200. Final OOP Mental Model#
Keep this model in your mind:
CLASS
│
defines a type
│
▼
OBJECT
│
┌────────┴────────┐
↓ ↓
STATE BEHAVIOR
│ │
fields methods
│ │
└────────┬────────┘
↓
OBJECT COLLABORATION
│
┌───────────┼───────────┐
↓ ↓ ↓
Encapsulation Abstraction Polymorphism
│
Inheritance
│
Interfaces
│
OOP Design
201. Final Summary#
In this chapter you learned:
- What OOP means
- Why OOP is useful
- Procedural programming
- Procedural vs OOP
- Class
- Object
- Instance
- State
- Behavior
- Object identity
- Object references
- new keyword
- Fields
- Methods
- Instance members
- Static members preview
- Reference types
- Primitive types
- Multiple objects
- Shared references
- Separate objects
- null references
- Default field values
- Field initializers
- Object lifecycle basics
- Object communication
- Responsibilities
- Encapsulation preview
- Abstraction preview
- Inheritance preview
- Polymorphism preview
- Composition preview
- IS-A
- HAS-A
- Cohesion preview
- Coupling preview
- Object-oriented modeling
- Real-world modeling examples
- Class design
- OOP mistakes
- Practical programs
- Design exercises
- Mini projects
- Interview questions
- Output questions
202. The Most Important Ideas to Remember#
If you remember only the most important points from this chapter, remember these:
1. A class defines a type.
2. An object is an instance.
3. One class can create many objects.
4. Objects have state and behavior.
5. Fields represent state.
6. Methods represent behavior.
7. A reference points to an object.
8. new creates an object.
9. Two references can point to the same object.
10. Two separate objects can have identical state.
11. == checks reference identity for objects.
12. Java has both primitive and reference types.
13. Good OOP assigns responsibilities to appropriate objects.
14. Encapsulation protects and controls state.
15. Abstraction hides unnecessary implementation details.
16. Inheritance creates a subtype relationship.
17. Polymorphism lets common types work with different implementations.
18. Composition models HAS-A relationships.
19. Inheritance should not be used just because it can reuse code.
20. Good OOP is about designing understandable, maintainable object collaborations.
203. Next Chapter#
You now understand the basic idea behind OOP.
The next step is to go deeper into the two most fundamental building blocks:
CLASSES
+
OBJECTS
Chapter 12 — Classes & Objects#
You will study:
Creating classes
Creating objects
Fields
Methods
Object references
Multiple objects
Instance state
Object identity
Memory/reference understanding
Object arrays
Objects inside objects
Object method calls
Passing objects to methods
Returning objects
Object equality
Object lifecycle
Common object mistakes
Practical OOP programs
After that, Chapter 13 will make object creation much more powerful using constructors.