Open/Closed Principle
Start course

In this course, we explore the SOLID principles one by one and look at how they work.

Learning Objectives

  • Understand what SOLID principles are

Intended Audience

  • Beginner Java developers
  • Anyone with a desire to learn about SOLID principles


  • Basic Java knowledge

Hello, dear friends. In this video, we will examine Open/Closed principles, let's begin. The Open/Closed principle, as OCP, is the SOLID principle which states that the software entities classes or methods should be open for extension but closed for modification. So, what does that mean?

This informs us that any code may change in the future due to new requirements. So, while coding to meet these requirements, we must protect all missions, otherwise this will be a major issue for our object. Essentially, any given class or module or function etc, should allow for its functionality to be extended but not allow for modification to its own source code. This principle aims to reduce the introduction of bugs and other errors into your code by requiring classes to not change their own implementation unless absolutely necessary as other derived or implemented classes may be relying on that implementation to function properly. It also wants us to implement classes that can easily have their functionality extended. And by allowing the class to be open for extension, we allow for many real world changes to occur without completely disrupting our design.

To better understand this, I think it's time to see this in code with an example. The most popular example is the rectangle and triangle area calculation. So, let's replicate this. Let's make a little console project. Create a new console project. You may give it any name. Now, first we will calculate areas of circles and rectangles. So, we need an interface named Shape. Because rectangle and circle are both shapes, interface Shape, now we need a method to calculate the area of Shape. Let's define CalculateArea method, done. As you know, an interface, we don't need any code block for methods because interface is a frame for classes. Now, let's define the Rectangle class. Now, this must be implemented from Shape. Now, we have to override CalculateArea method. But first, for the Rectangle we need height and length parameters. Done, let's generate getters and setters. Now, let's calculate the area of the rectangle, area equals, done.

Now, let's define Circle class. Implements from Shape. First, we need a Radius parameter for the circle area. Let's generate getters and setters. Now, let's override Calculate method, let's CalculateArea. Done. Now, in the main method, first I want to define a Shape List. Now, I will add Rectangles and Circles in the list. Now, as you see, we have two rectangles and two circles.

Now, with the loop, we can calculate all these shapes area and show them to the users. We need a loop, you can use a for loop or a while loop. Let's show it on screen. Done, let's try. Look, we can see the result. Actually, this is kind of enough for us but I'd like to demonstrate more information about Open/Closed because OCP is the most important principle in an EE project.

We'll make a new design for this operation. In this class, we need three parameters. Let's define Length, Height, and Radius. Also, we need another parameter for Type of shape and Type. Generate getters and setters, done. Let's add a new class, the name will be ShapeType. Let's add shapes as static. Rectangle = 1, Circle = 2. We don't need to add any getters or setters. Now, we need a new method named CalculateArea2. Now, in this method we will calculate. First we need a condition to determine type of Shape, if ShapeType equals Type Rectangle. We have to calculate the area of the Rectangle, else if TypeCircle now calculate circles area. Now, in the main method, let's call this method.

But first, let's comment these lines out. Now, we need a new list. Create an instance from AllShape class. Let's set values for a Rectangle. Add to list. Now let's add a circle. Done, now with the loop, we can call this method. After the loop we need an if else statement to print the result. Done, let's run the application. Look, we can see the result. The project is working without any issues. However, we also want to add and calculate the area of a triangle. So, we need OCP. If we change the rectangle to a triangle, we can calculate the triangle, but we cannot calculate the rectangle. So, we have to add a new condition. First, in the ShapeType class we need a new Type named Triangle.

Now, in the CalculateArea method, we also need a new condition, else if Triangle. Now calculate area. Done, this will be enough for this method. Let's add a triangle to our list. Run, we can also see the triangles outcome now. You may be wondering why I'm showing you this, it's because you need this condition structure in an EE project to authorize operations. For example, you may need to hide some operations, such as the 'Add' button from a standard user. In that case, you must use this condition structure. But in the first method, adding a triangle is also very easy.

Let's comment these lines out and uncommon the previously commented lines. Now in the first method we need a new class called Triangle, implements shape. Let's define parameters, override calculate method. This will be enough for us. Let's add a Triangle to our first list. Let's run. Look, now we can see all areas. So, I hope you have a better understanding of the Open/Closed principle. In the next video, we will examine the L letter of SOLID which stands for Liskov Substitution Principle. See you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.