Interface In Java: If you follow our core java tutorial series, then you can find that in our last post, we have discussed the abstract class in java by which we can achieve partial abstraction, but if we want to achieve the full abstraction then we have to use interface.
Before going through interface, let me share with you about Abstraction: it’s a process where you are displaying to the user what a user wants to see and hide the unnecessary things to the outer world. In this post, we are going to learn about what is an interface in java and why we need to use interface in the Java programming language.
What Is Interface in Java?
The interface is nothing like a class that has variables and methods, but the methods don’t have any method body. By default, all the methods of an interface are an abstract method (only method signature) — the variables of an interface by default public, static, and final.
By creating an interface, we have created a rule that what are the things need to do by the implementation class. If a class implements an interface, then that class needs to implement all the methods of that interface; otherwise, the class should be declared as abstract.
Interface Syntax
interface <interface_name> { // declare constant fields // declare methods that abstract // by default. }
For creating an interface, you have to use the keyword “Interface”, we can achieve abstraction fully. As we mentioned above those class wants to use the interface they have to use the keyword implements and also need to provide method body to all the mentioned methods of the interface.
Advantages of Interface
- By implementing interface concepts of Java programming language, we can achieve total abstraction.
- As we know that due to of ambiguity problem java does not support multiple inheritances, but by using an interface in our programming language, we can achieve multiple inheritances.
Example Of Interface
interface MyInterface { /* compiler will treat them as: * public abstract void method1(); * public abstract void method2(); */ public void method1(); public void method2(); } class Demo implements MyInterface { /* This class must have to implement both the abstract methods * else you will get compilation error */ public void method1() { System.out.println("implementation of method1"); } public void method2() { System.out.println("implementation of method2"); } public static void main(String arg[]) { MyInterface obj = new Demo(); obj.method1(); } }
How does the interface work in Inheritance?
According to the Java programming language, an interface can not implement another interface, but an interface can extend another interface.
Note: If there are two interfaces, like interface1 and interface2, and there is a class that is implemented both the interface, then the class should have implemented all the methods of both the classes.
interface Inf1 { public void method1(); } interface Inf2 extends Inf1 { public void method2(); } public class Demo implements Inf2 { /* Even though this class is only implementing the * interface Inf2, it has to implement all the methods * of Inf1 as well because the interface Inf2 extends Inf1 */ public void method1() { System.out.println("method1"); } public void method2() { System.out.println("method2"); } public static void main(String args[]) { Inf2 obj = new Demo(); obj.method2(); } }
Tag or Marker interface in Java
If an interface doesn’t have any method or variable, then such type of interface is called a tag or marker interface. the example of marker interface is Remote, EventListener, and Serializable.
Essential Points about Interface in Java
- We Can’t create an instance of an interface.
- We are using the keyword interface to create an interface in the Java programming language.
- Because of the interface, we can get a total of 100% abstraction, in our last post we have seen that we can able to achieve partial abstraction because of Abstract class in Java. Still, due to the interface, we are getting total abstraction because in interface methods don’t have a method body.
- Interface can’t have constructors because we can not initiate an interface, and also the methods don’t have a method body.
- By default, the variables which are declared inside an interface are public, static, and final.
- The interface methods are, by default, abstract, and public.
- An Interface can’t extend another class, but it extends another interface.
- When a class wants to use an interface at that time, we are using the keyword “Implements.”
- If a class implements an interface, then all the methods of the interface should be implemented else, we need to mark that class as abstract.
- An Interface cannot be declared as private, protected, and transient.
- All the Variables must be initialized at the time of declaration inside interface; otherwise, you will get a compile-time error.
- In the implemented class you can not change the value of the interface variable because by default the variables of an interface are public, static and final
- If two interfaces have the same method, then only once implementation is enough.
- A class can not implement two methods which have the same name and different return type.
- We can resolve the variable name conflicts with the help of the interface name.
Implement Interface In Java Program: How to Declare and Implement Interface in java with example programs?
package com.java.Softwaretestingblog; interface AA { public void m1(); } interface BB { public void m1(); } public class IterfaceExample implements AA,BB { public void m1() { System.out.println("interface"); } public static void main(String[] args) { // TODO Auto-generated method stub IterfaceExample obj=new IterfaceExample(); obj.m1(); } }
Output:
interface
Another Example:
package com.java.Softwaretestingblog; interface InterfaceExBase { void open(); void close(); }</pre> <pre class="lang:default decode:true " title="InterfaceImplementclass">package com.java.Softwaretestingblog; public class InterfaceImplementclass implements InterfaceExBase { public void open() { System.out.println("Open Method Executed"); } public void close() { System.out.println("Close Method Executed"); } public static void main(String[] args) { // TODO Auto-generated method stub InterfaceImplementclass obj=new InterfaceImplementclass(); obj.open(); obj.close(); } }
Output:
Open Method Executed Close Method Executed
Ref: article
Leave a Reply