solid open closed principle example java

Mastering SOLID: Open-Closed Principle in Java

By Laura Moore
AD

The Solid, Open-Closed Principle

The Open-Closed Principle (OCP) is a design principle that advocates for software entities to be open for extension but closed for modification. It's one of the fundamental principles of Object-Oriented Programming (OOP). Calling it "solid" refers to the SOLID principles, a crucial set of object-oriented design principles introduced by Robert C. Martin.

The OCP forms an essential aspect of extensible and maintainable code. It promotes adhering to the Liskov substitution principle to ensure that a derived class (could be a subclass) must be substitutable with its base class without affecting the correctness of the program.

Closed for Modification - A Java Example

In Java, classes should be closed for modification to ensure that modifying or adding behaviors does not break existing code. Here's an example of a closed class:

AD
SOLID Principles Explained: A Visual Guide to Better System Design
SOLID Principles Explained: A Visual Guide to Better System Design
SOLID: Part 2 - The Open/Closed Principle | Envato Tuts+
SOLID: Part 2 - The Open/Closed Principle | Envato Tuts+
Liskov Substitution Principal explained with examples in Java
Liskov Substitution Principal explained with examples in Java
```java public class Logger { public void log(String logMessage) { System.out.println(logMessage); } } ```

This Logger class is closed for modification. We cannot introduce changes in the logging behavior without modifying the Logger class itself, hence adhering to the 'closed' part of the OCP.

Open for Extension - A Java Example

A class can be open for extension by providing extension points, allowing derived classes to alter the behavior. Let's see an example:

SOLID - The First 5 Principles of Object Oriented Design
SOLID - The First 5 Principles of Object Oriented Design
Java OOP Concepts Explained | Class, Object, Encapsulation, Inheritance, Polymorphism & Abstraction
Java OOP Concepts Explained | Class, Object, Encapsulation, Inheritance, Polymorphism & Abstraction
an open closed pringle diagram with several different types of words and numbers on it
an open closed pringle diagram with several different types of words and numbers on it
```java public abstract class Shape { public abstract float getArea(); public final void drawShape() { System.out.println("Shape is drawn."); } } public class Rectangle extends Shape { @Override public float getArea() { // Rectangle specific area calculation logic return 0f; } @Override public void drawShape() { System.out.println("Rectangle is drawn."); } } ```

The abstract Shape class is open for extension. We cannot change the existing code (closed), but we can extend the behavior by introducing new shapes.

The Open-Closed Principle and Polymorphism

The OCP is deeply rooted in polymorphism, another vital principle of OOP. Variation in how the same code behaves in different situations or through different objects fitting a common interface is polymorphism.

AD

Let's see how a 'closed' and 'open' system can interact and exemplify OCP with polymorphism:

Revisiting SOLID principles
Revisiting SOLID principles
SOLID: Part 3 - Liskov Substitution & Interface Segregation Principles | Envato Tuts+
SOLID: Part 3 - Liskov Substitution & Interface Segregation Principles | Envato Tuts+
an open / closed sign with the words pringle ocf
an open / closed sign with the words pringle ocf
Master Clean Code: The SOLID Principles πŸ›οΈπŸ’»
Master Clean Code: The SOLID Principles πŸ›οΈπŸ’»
Object-Oriented Software Principles
Object-Oriented Software Principles
The Open-Closed Principle at an Architectural Level
The Open-Closed Principle at an Architectural Level

Polymorphism in Practice

Here's an example of how Java's polymorphism can help maintain 'closed' and 'open' principles:

```java interface Animal { void makeSound(); } class Dog implements Animal { @Override public void makeSound() { System.out.println("Dog barks"); } } class Cat implements Animal { @Override public void makeSound() { System.out.println("Cat meows"); } } ```

Maintainers of the Animal interface can't alter its signature, so it's 'closed'. Yet, we can add or alter ways to `makeSound` by introducing new Animal implementations, opening up the system. Therefore, we can learn to write code that maximally supports the Open-Closed Principle.

AD

Comprehending and applying the Open-Closed Principle helps shape our code into extensible modules that can handle new requirements without resorting to extensive refactoring. Adequate knowledge of this principle can be a real game-changer when building and maintaining large-scale software systems.

SOLID Principles In C# - Single Responsibility Principle
SOLID Principles In C# - Single Responsibility Principle
an image of a diagram that shows how to use the apis for web development
an image of a diagram that shows how to use the apis for web development
Cloning in Java | Shallow Copy and Deep Copy tutorial with examples
Cloning in Java | Shallow Copy and Deep Copy tutorial with examples
SOLID Principle in Programming: Understand With Real Life Examples
SOLID Principle in Programming: Understand With Real Life Examples
an image of different types of logos in the language of webpages and apps
an image of different types of logos in the language of webpages and apps
a purple screen with the text closure
a purple screen with the text closure
Java Loops Explained | for, while, do-while, Nested & for-each Loop (Java Series #6)
Java Loops Explained | for, while, do-while, Nested & for-each Loop (Java Series #6)
the flow diagram for how java works is depicted in this graphic, which shows an image of a person working on a computer
the flow diagram for how java works is depicted in this graphic, which shows an image of a person working on a computer
Brij kishore Pandey on LinkedIn: SOLID Principles: The Foundation of Modern Software Design In the… | 32 comments
Brij kishore Pandey on LinkedIn: SOLID Principles: The Foundation of Modern Software Design In the… | 32 comments
Java Basics for Beginners πŸš€
Java Basics for Beginners πŸš€
Object Cloning in Java | Clone() Method, Example - Scientech Easy
Object Cloning in Java | Clone() Method, Example - Scientech Easy
Top Features of Java Every Beginner Should Know β˜•πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€
Web Development programing tricks and tips for beginners free
Web Development programing tricks and tips for beginners free
AD