1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Exploring the SOLID Principles in Java



SOLID Principles
What is SOLID?
15m 18s

The course is part of this learning path

Start course

This course looks at the five SOLID principles starting with a little bit of their history before moving on to cover each principle in more detail.

Learning Objectives

  • Learn about the five SOLID principles:
    • Single Responsibility
    • OpenClosed
    • Liskov Principle
    • Interface Segregation
    • Dependency Inversion

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE.


Basic knowledge of Java programming.


Hello dear friends. In this video, we will examine Open/Closed principle. So, 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 it 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 not to 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, I think it's time to see this in code with an example. The rectangle and shape example is the most chosen example. So, let's create 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 or rectangles. So, first we need an interface named shape because rectangle and circle are both shapes, interface shape. Now we need a method to calculate area of shape.

Let's define CalculateArea method. Done. As you know, in interface, we don't need any code block for methods because interface is a frame for classes. Now, let's define rectangle class. Now this must be implements from shape. Now we have to override calculate area method. For rectangle, we need height and length parameters. Done. Let's generate getters and setters. Now, let's calculate area of rectangle. Done. Now let's define circle class, implements from shape. First, we need a radius parameter for circle area. Now, let's override calculate method. Let's calculate area. Done. Now, in main method. First, I want to define a shape list. Now, I will add rectangles and circles in list. Now, as you see, we have two rectangles and two circles. Now with the loop, we can calculate all these shapes' area and we can show them to the users. We need a loop you can use for or-while.

Let's show on screen. Done, let's try. Look, we can see the result. Actually, this will be enough for us but I want to show more information about Open/Closed because in EE projects, the most important principle is OCP. First, let's define a new class name, AllShape. We will make a new design for this operation. In this class, we need tree parameters. Let's define length, height and radius. Also, we need another parameter for type of shape in type. Generate getters and setters. Done. Now, let's add a new class name that 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(Type == ShapeType.Rectangle), we have to calculate area of rectangle, else if TypeCircle. Now calculate circle's area. Now, in main method, let's call this method first, make comment line these lines. First, we need a 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 loop, let's show. Done. Let's try. Look, we can see the result. Now, project is working without any problems. But, for example, we want to add triangle also to calculate so we need OCP. If we change rectangle for triangle, we can calculate triangle but we cannot calculate rectangle. So, we have to add a new condition.

First, in type class, we need a new type named Triangle. Now, in calculate area method, we also need a new condition. else if(Type == ShapeType.Triangle). Now, calculate area. Done. This will be enough for this method. Let's add a triangle to our list. Run. Look, now we can see triangle's results also. You may think that is why I'm showing this because in EE projects and authorization operations, you will need this condition structure. For example, you will need to hide some operations from standard user like 'Hide Add' button. So, in that case, you have to use this condition structure. But in first method adding, a triangle is very easy also. Let's make them comment lines. Now in first method, we need a new class called Triangle, implements from shape. Override calculate method. Let's define parameters. This will be enough for us. Override calculate method. Let's add a triangle to our first list. Let's run. Look, now we can see all areas. I hope you now have a better understanding of the Open/Closed principle. In the next video, we will examine L letter of solid which stands for List Cost 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.

Covered Topics