Java Interfaces and their implementation in real-world

About Java Interfaces

In Java, there are certain situations where the programmer just needs to focus on which object is doing the job and providing desired output. Java Provides a great tool for this, which is known as an Interface.

Although Class and Interfaces look similar, however, they have differences between them.

Parameter Class Interface
Definition A class is defined as a blueprint of an object. An Interface is defined as the blueprint of a   class that contains abstract and static methods.
Function A class defines an Object's state,   attributes, and behavior (Real World   Entities). An Interface defines a Job / Purpose.
Methods to Access Classes are Inherited from another class using extends keyword. Interfaces are implemented in classes using the implements keyword.
Body Classes have their own body with function definitions. Interfaces don't have a body of their own. They can only contain the declaration of abstract and static methods. These methods are overridden in subclasses.
Relation A class can implement an interface. An Interface cannot extend a Class.


Java Interface Implementation

From the above table, you must have known that Interfaces are the blueprint of the class itself. Which in turn, is a blueprint of an object. Through one class we can make infinite no of objects of its type. But through interfaces, we can make infinite classes, each of them can be unique as well as similar at the same time.

Let us consider a real-life scenario of a smartphone. You must have seen that today there are thousands of smartphones available on the market. They all have the same features which a smartphone offers such as: -

  1. Camera Functionality
  2. Internet Functionality
  3. Office Applications
  4. Multimedia Functionality
  5. Games Functionality

Basic Functionalities are almost the same in every smartphone. Yet what is the factor which makes them different and unique from each other? Have you ever thought about it?

It is the implementation of those features which makes them unique.

Designing a Smartphone Interface

Let us Suppose we have to design an Interface for two mobile brands say Xiaomi and Samsung Galaxy.

Now start thinking, both of them are nothing but smartphones. Hence, they will have the same features just their implementation will be unique, and features will be the same.

For Example, the Samsung Galaxy Phone provides Camera Feature. Similarly, the Xiaomi

The smartphone also has the same feature just a difference in versions, the hardware used, etc.

Here will we create 5 Common Interfaces (or features). They are as follows:

  1. Camera Feature
  2. Multimedia Feature
  3. Internet Browsing Feature
  4. Games
  5. Office Features

Java Interfaces

An Illustration of Interface Implementation in smartphones

Let us start declaring them one by one. Please note we are not going to code a smartphone operating system here. We are just designing a dummy interface for a smartphone to understand how interfaces are used and implemented. Each interface will contain a simple message just to show the user that the particular feature is present in the smartphone.

We have created the following interfaces:

interface Browser{
    public void browse(String bName,String URL);
}

interface Camera{
    public void cam(String pixels,String operation);
}

interface Games{
    public void play(String gName);
}

interface Multimedia{
    public void start(String appName,String media);
}

interface Office_Tools{
    public void open(String appName,String file_type);
}

Section of Smartphone

interface Browser{
    public void browse(String bName,String URL);
}

interface Camera{
    public void cam(String pixels,String operation);
}

interface Games{
    public void play(String gName);
}

interface Multimedia{
    public void start(String appName,String media);
}

interface Office_Tools{
    public void open(String appName,String file_type);
}

public class Smartphone implements Browser,Camera,Games,Multimedia,Office_Tools{

    @Override
    public void browse(String bName,String URL){
        System.out.println("Starting "+bName+" Browser");
        System.out.println("Opening Site : "+URL);
    }

    public void cam(String pixels,String operation){
        System.out.println("Camera Configuration is "+pixels+" Mega Pixels");
        if(operation == "Photo"){
            System.out.println("Camera Clicked Photo");
        }
        else if(operation == "Photo"){
            System.out.println("Camera Recorded Video");
        }
        else 
        System.out.println("Camera can only click/record a photo/video.");

    }

    public void play(String gName){
        System.out.println("Starting Game :"+gName);
    }

    public void start(String appName,String media){
        System.out.println("Opening "+appName+" Application");
        System.out.println("Opening "+media);
    }

    public void open(String appName,String file_type){
        System.out.println("Starting : "+appName);
        System.out.println("Opening : "+file_type+" file");
    }
}

Code Explanation

Here we have developed a class named Smartphone which implements all the interfaces (features) that every smartphone brand must have. All the interfaces are blueprints (or we can say designs) of a class Smartphone. Now using this smartphone class, a blueprint/design/architecture we can create n no. of unique Smartphones with the same functionalities implemented in a different way.

Let us see how we will do it.

  1. Firstly, we will be creating a new class named Samsung Galaxy (A popular smartphone).
  2. Then we will be inheriting the Smartphone class to Samsung Galaxy.
  3. Then we will be calling all the abstract functions of all interfaces with their required arguments.
  4. Then we will create another class Xiaomi and inherit the Smartphone to it as we did earlier.
  5. Then we will be repeating Step 3 but with different arguments in order to represent Xiaomi’s Uniqueness as Compared to Samsung Galaxy.

Code Section Samsung_Galaxy

import java.util.Scanner;

public class Samsung_Galaxy extends Smartphone {
    public static void main(String args[]){
        Scanner bc = new Scanner(System.in);

            System.out.println("Welcome to Samsung Galaxy");
            System.out.println("Press 1 for Camera");
            System.out.println("Press 2 for Internet");
            System.out.println("Press 3 for Office");
            System.out.println("Press 4 for Games");
            System.out.println("Press 5 for Multimedia");
            System.out.println("Press 6 for Shutdown");
            int choice = bc.nextInt();
            Smartphone obj = new Smartphone();
            switch(choice){
                case 1:
                obj.cam(13,"photo");
                obj.cam(13,"video");
                break;
                case 2:
                obj.browse("Chrome","https//:www.c#corner.com");
                break;
                case 3:
                obj.open("Google Docs", "PDF");
                break;
                case 4:
                obj.play("Grand Theft Auto : Vice City");
                break;
                case 5:
                obj.start("VLC Media Player","Rainbow.mp3");
                break;
                case 6:
                System.out.println("Shutting Down...");
                bc.close();
                break;
                default:
                System.out.println("Invalid Choice !");
            }

    }
}

Code Section Xiaomi

import java.util.Scanner;

public class Xiamo extends Smartphone {
    public static void main(String args[]){
        Scanner bc = new Scanner(System.in);

            System.out.println("Welcome to Samsung Galaxy");
            System.out.println("Press 1 for Camera");
            System.out.println("Press 2 for Internet");
            System.out.println("Press 3 for Office");
            System.out.println("Press 4 for Games");
            System.out.println("Press 5 for Multimedia");
            System.out.println("Press 6 for Shutdown");
            int choice = bc.nextInt();
            Xiamo obj = new Xiamo();
            switch(choice){
                case 1:
                obj.cam(18,"photo");
                obj.cam(18,"video");
                break;
                case 2:
                obj.browse("Firefox","https//:www.c#corner.com");
                break;
                case 3:
                obj.open("Office 365", "Docx");
                break;
                case 4:
                obj.play("Candy Crush Soda Saga");
                break;
                case 5:
                obj.start("Pi Music Player","Alone.mp3");
                break;
                case 6:
                System.out.println("Xiamo is Shutting Down...");
                bc.close();
                break;
                default:
                System.out.println("Invalid Choice !");
            }

    }
}

The output of Samsung_Galaxy:

Java Interfaces

Java Interfaces

The output of Xiaomi:

Java Interfaces

Java Interfaces

Conclusion

Summing up everything, we can conclude the following points: -

  1. An object is a unique entity that has some state, attributes as well as behavior. It is a physical entity.
  2. Class is a Blueprint of an object, i.e., It can be thought of as a design by which different objects of similar type are created. It is a logical entity. It doesn’t have a physical existence of itself like an object.
  3. Interfaces are the Blueprint of a class and can be considered the Big Picture.
  4. Interfaces Define the behavior of the class itself whereas a class defines the behavior of an object.
  5. Interfaces in Java are a great tool that supports Multiple Inheritance.
  6. The interface cannot contain its own body, they only contain declarations of abstract fields and functions that are to be overridden in subclasses.
  7. The Idea behind Interface is to allow every subclass to use the inherited method in its own desired way i.e., Polymorphism.
  8. Interfaces are dependent on the classes.
  9. Classes are independent of everything.
  10. Classes focus on Entity Structure whereas Interfaces emphasize Entity’s behavior.