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

Interface Segregation

Contents

keyboard_tab
SOLID Principles
1
What is SOLID?
PREVIEW2m 11s
2
3
OpenClosed
15m 18s

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
32m
Students
6
Description

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.

Prerequisites

Basic knowledge of Java programming.

Transcript

Hello dear friends. In this video, we will examine the Interface Segregation principle. So, let's begin. The Interface Segregation principle states that no client code object should be forced to depend on methods it does not use. Basically, each code object should only implement what it needs not to be required to implement anything else. For instance, if you have a phone and need to charge it, you don't truly need this adapter, but you got it anyway. Right now, you'll only use one connection, the rest are unnecessary. Alternatively, let's say you have a dishwasher with a USB port. Why though do we not use a USB connection for the dishwasher? This principle tells us to only use interfaces that are truly necessary, otherwise, our code will not conform to the single responsibility rule.

Now, let's see this in a code. Let's say for example, we have a market that sells food and clothes. Now let's create an interface and name it Product. Now add some properties name, ProductMark, Salary, Size for clothes like small, large, and Calorie for foods. Now Let's derive first class for foods. For foods, we don't need size, so don't override. Now as you see, we have an error. See, solution is implementing interface method, implement interface methods. Now, no error. Let's add another class for clothes. Calorie is unnecessary for this class. Now see, we have an error again. So, we have to override Calorie. So, there is no error now, but it is not a good structure because size is unnecessary in the Food class and Calorie property is unnecessary for clothes. As a result, we must redesign this structure.

First, we will create a new interface and name it Base. Now the property's name, Mark and Salary are necessary for all products. Now if you want, you can use this only, and add calorie, and size, and class. But for big projects there will be many properties. So, let's make them with interfaces. New interface named IFood. Add Calorie. Now another interface and name it IClothes and add size. Now, if you want, define a Meal class derived from Base and Food interface. Implement interface. Now add another one and name it trousers; derives from Base and Clothes interface. Implement interface. Now look again. As you see, we don't have any unnecessary properties in classes. That's all for the Interface Segregation Design Pattern. It's a really important design pattern, just like the Adapter Design Pattern. In the next video, we will examine the Dependency Inversion principle. See you in the next video.

 

About the Author
Students
413
Courses
35
Learning Paths
2

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