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: -
- Camera Functionality
- Internet Functionality
- Office Applications
- Multimedia Functionality
- 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:
- Camera Feature
- Multimedia Feature
- Internet Browsing Feature
- Games
- Office Features
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.
- Firstly, we will be creating a new class named Samsung Galaxy (A popular smartphone).
- Then we will be inheriting the Smartphone class to Samsung Galaxy.
- Then we will be calling all the abstract functions of all interfaces with their required arguments.
- Then we will create another class Xiaomi and inherit the Smartphone to it as we did earlier.
- 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:
The output of Xiaomi:
Conclusion
Summing up everything, we can conclude the following points: -
- An object is a unique entity that has some state, attributes as well as behavior. It is a physical entity.
- 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.
- Interfaces are the Blueprint of a class and can be considered the Big Picture.
- Interfaces Define the behavior of the class itself whereas a class defines the behavior of an object.
- Interfaces in Java are a great tool that supports Multiple Inheritance.
- The interface cannot contain its own body, they only contain declarations of abstract fields and functions that are to be overridden in subclasses.
- The Idea behind Interface is to allow every subclass to use the inherited method in its own desired way i.e., Polymorphism.
- Interfaces are dependent on the classes.
- Classes are independent of everything.
- Classes focus on Entity Structure whereas Interfaces emphasize Entity’s behavior.