Introduction to Interface in Java
The Interface is a medium to interact between user and system devices. For example, in our real life, if we consider the interface to be given as an example is the air condition, bike, ATM machine, etc. Similarly, in Java, Interface also plays the same role; it is considered as an interactive medium between the system and the end-user. However, the main aim of the interface in java is to achieve 100% abstraction by hiding actual implementation to the user and thus making applications a bit user-friendly. Therefore, it basically comprises of static, abstract, and final methods.
Syntax:
interface <interface name> {
// declare constant variables
// declare abstract methods
// declare default modifier
// by default
}
Why do we need it?
There are three main reasons why do we need it.
- The first need is to make the application very user-friendly, which can be done by making two different systems communicate via each other smoothly via interface [i.e., making a java application talk with the mainframe systems interface.
- Applications will be enhanced and optimized as well as simplified as its internal implementation is not the concern of the end-user.
- The most important of all is that the application becomes secured as the implementation after making abstract is hidden from the outside world, thus maintaining the security aspects as well.
- The functionality of the multiple inheritances also becomes supportable.
- As it is most often defined as IS-A-Relationship, it is used to achieve loose coupling.
How does the interface work in Java?
If we see a bigger picture of java, then the interface plays a pivotal role in how any service requirement specification is considered an interface. The client-side perspective of expecting a set of services to be delivered to the end-user is known as interface, and from the perspective of service providers, the set of services being offered is known as the interface.
Therefore, as mentioned earlier with a real-life example, an interface in java is nothing but a contract between the client-side and service providers. The abstract class consists of both abstract class as well as normal methods or non-abstract classes, but the case is not the same with the interface. It has really helped the main framework of applications make them secured and came a lot to become user-friendly. It maintains IS-A-Relationship, which also helps make the inheritance and extend between classes possible, thus providing visibility.
The nested interface is yet another type of interface that we can suggest by its name as an interface within an interface and comes as a part of nested classes as well.
How to Declare Interface in Java?
By declaring interface keyword: We can declare an interface by using the interface keyword and implement it by using the implement keyword.
Example:
interface Interf
{
void m1(); // by default public abstract void m1();
voidm2();
}
Implementation of Interface in Java
Implementation of an interface in java is performed by using implements keywords.
interface Interfc
{
void m1();
void m2();
}
abstract class ServiceProvider implements Interfc
{
public void m1()
{
}
}
Therefore, if a class implements an interface mandatorily, we should provide an implementation for every method of that interface; otherwise, that class must be declared as abstract. If an interface is getting implemented, it must declare the interface as public; otherwise, it will throw an error saying compile-time error.
Then there is one more essential aspect to be followed with interface declaration and implementation.
Examples to Implement Interface in Java
Following are the examples of implementing an interface in java with different scenarios:
Example #1
Code:
public class Cat implements Animal {
public interface Animal
{
public void animalRuns ();
public void Sleep ();
}
public void animalRuns ()
{
System.out.println("The cat says meow meow");
}
public void Sleep()
{
System.out.println("cat sleeps");
}
public static void main(String[] args)
{
Cat myCat = new Cat();
myCat.animalRuns();
myCat.Sleep();
}
}
Output:
The above output tries to prove the point that the interface must be implemented by another class using the implements keyword rather than using the extends keyword.
Example #2
Implementation of Multiple Interfaces:
Code:
public class DemoClass implements frstInterface, secInterface {
public interface frstInterface
{
public void myMethod ();
}
public interface secInterface
{
public void scnInterface();
}
public void scnInterface () {
System.out.println("Some random Text");
}
public void myMethod() {
System.out.println("Some useful Text");
}
public static void main (String[] args) {
DemoClass myDemo = new DemoClass();
myDemo.myMethod();
myDemo.scnInterface();
}
}
Output:
Example #3
One interface can extend another interface also once a class can implement an interface.
Code:
public class TestImpl4 implements showble
{
public interface Printable
{
void print();
}
public interface showble extends Printable
{
void show();
}
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome to the project");
}
public static void main(String[] args)
{
TestImpl4 obj = new TestImpl4();
obj.print();
obj.show();
}
}
Output:
Conclusion
Many times, people get confused between abstract class and interface, but they are different. An interface contains only an abstract method, but java 8 can accommodate both default and static methods also which is like add-on. The interface gives its 100% and can achieve full abstraction, but this is not the case with the abstract class, which provides partial abstraction only.
Recommended Articles
This is a guide to Interface in Java. Here we discuss the introduction and how does the interface work in java, along with examples to implement an interface. You may also look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses