Solid Principles in Java

Chamali Basnayake
2 min readAug 1, 2020

S.O.L.I.D principle is mainly used to write code in an efficient and effective way so that can be managed easily. Additionally it supports for agile software development.

S: Single Responsibility Principle

Key Idea: One class should have only one responsibility

Simply define as one class should have one task to do

This causes to create fewer functions

Lesser functions lead for easy testing

Powerful code organization

O: Open Closed Principle

Key Idea: Software components should only opened to extend, but closed to modify

This extending can be achieved by inheriting from a class or overwriting the required behaviors from a class or by extending certain behaviors of the class

This principle recommends using interfaces

L: Liskov Substitution Principle

Key Idea: All derived types should be completely substitutable to the respective base types

Every sub class or derived class should be substitutable for their parent or base class

This principle avoids misuse of inheritance by conforming to “is-a” relationship

I: Interface Segregation Principle

Key Idea: Users should not implement unnecessary methods which will not be used

According to this principle users should be forced to concern on methods which are not in use

Very large interfaces are segregated into specific and small interfaces so that users only have to know about the methods that are interested

These types of segregated interfaces are called role interfaces

D: Dependency Inversion Principle

Key Idea: Depends on abstractions not on concretions

High level module should not depend on any low level module

According to this principle, it removes hard coded dependencies so that program can be extendable

SOLID Principles

--

--