OOP · Advanced OOP
Immutable Objects
Difficult to debug if used extensively.
Limitations
- Cannot be reused.
- Difficult to debug if used extensively.
- Not suitable when multiple method calls are required on the same object.
Real-World Problem
Imagine you're developing a Banking System where each customer has an Account Number. Once an account number is assigned, it should never change because modifying it could lead to incorrect records, security issues, and data inconsistency. Java solves such problems using Immutable Objects. Why Do We Need Immutable Objects?
Many applications contain data that should remain unchanged after creation, such as account numbers, employee IDs, Aadhaar numbers, or transaction IDs. Allowing these values to change can introduce bugs and security risks. Immutable objects ensure that once an object is created, its state remains constant throughout its lifetime.
They also simplify concurrent programming because multiple threads can safely access the same immutable object without synchronization. What is an Immutable Object?
An Immutable Object is an object whose state cannot be modified after it has been created. Once the object is initialized, its data remains constant. If different values are required, a new object must be created instead of modifying the existing one. The String class is one of the most common examples of an immutable object in Java.
Characteristics
- Object state cannot change after creation.
- Fields are usually declared as private and final.
- Values are initialized through constructors.
- Setter methods are not provided.
- Only getter methods are used to access data.
- Objects are inherently thread-safe.
How to Create an Immutable Class To create an immutable class:
- Declare the class as final.
- Make all instance variables private and final.
- Initialize variables using a constructor.
- Do not provide setter methods.
- Provide getter methods to read values.
- If the class contains mutable objects, return defensive copies instead of the original objects.
Creating an Immutable Class
Mutable vs Immutable Object

String Immutability


When to Use Use immutable objects when:
- Data should never change after creation.
- Security is important.
- Multiple threads access the same object.
- Objects represent fixed values or identities.
Advantages
- Thread-safe by default.
- Improves application security.
- Prevents accidental modification.
- Simplifies debugging.
- Easy to maintain.
- Frequently used in enterprise applications.
Limitations
- New objects must be created whenever values change.
- May increase memory usage if many new objects are created.
- Not suitable when object data changes frequently.
Real-World Problem
Imagine you're developing a simple E-Commerce application. Initially, the application contains only a few classes such as Product, Customer, and Order. Since the project is small, writing all the logic inside a few classes seems easy and convenient.
As the business grows, customers request new features such as multiple payment methods, discount coupons, invoice generation, notifications, product recommendations, and order tracking. Developers continue adding these features to the existing classes without following any design guidelines.
After a few months, the application becomes difficult to maintain. A small change in one module unexpectedly affects several others. Bugs become more frequent, testing requires more effort, and introducing new features takes much longer than before. Although the application still works, its design gradually becomes more complex and harder to manage.
What Are SOLID Principles?
SOLID is a collection of five object-oriented design principles that help developers build software that is easier to understand, maintain, test, and extend. Instead of focusing only on making a program work, SOLID encourages developers to design software that remains flexible as business requirements change over time.
These principles provide practical guidelines for organizing classes and objects so that each component has a clear responsibility, dependencies are managed effectively, and new features can be added with minimal impact on existing code. As a result, applications become more scalable, reusable, and easier for teams to develop collaboratively.
Who Introduced SOLID?
The ideas behind the SOLID principles were introduced by Robert C. Martin, widely known as Uncle Bob, an American software engineer and author recognized for his contributions to software architecture and clean code practices. During the early 2000s, he brought together five object-oriented design principles that help developers create maintainable and extensible software. The acronym SOLID was later popularized by Michael Feathers, making these principles easier to remember and widely adopted by the software development community.
Why Were SOLID Principles Introduced?
As software projects evolve, new features, changing business requirements, and growing codebases often make applications increasingly difficult to manage. Without proper design principles, classes tend to become overloaded with responsibilities, components become tightly coupled, and modifying one part of the system can unintentionally affect many others.
SOLID principles were introduced to address these challenges by providing a structured approach to object-oriented design. They encourage developers to create software that can evolve without requiring major changes to existing code. By following these principles, applications become easier to maintain, simpler to test, and more adaptable to future requirements.
Why Do We Need SOLID Principles? Modern software is expected to evolve continuously. New features are added, existing functionality changes, and applications must adapt to new technologies and customer expectations. If the software design is not flexible, every modification increases complexity and the risk of introducing defects.
SOLID principles help developers design systems that can accommodate change with minimal effort. They promote high cohesion, low coupling, and clear separation of responsibilities, making applications easier to understand and extend. These principles are especially valuable in enterprise applications where multiple developers work on the same codebase and long-term maintainability is essential.
Benefits of SOLID Principles
- Improves code readability and organization.
- Makes software easier to maintain and extend.
- Reduces tight coupling between classes.
- Encourages reusable components.
- Simplifies unit testing and debugging.
- Supports scalable software architecture.
- Reduces the risk of introducing bugs during modifications.
- Enables teams to collaborate more effectively on large projects.
The Five SOLID Principles
- S – Single Responsibility Principle (SRP)
- O – Open Closed Principle (OCP)
Make state final and expose no setters so an object can never change after construction.
public final class Money {
private final int amount; // set once
private final String currency;
public Money(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}
// Operations return NEW objects instead of mutating
public Money plus(int extra) {
return new Money(amount + extra, currency);
}
public int amount() { return amount; } // getters only
}