Interface In Java: If you follow our core Java tutorial series, you can find that in our last post, we discussed the abstract class in Java by which we can achieve partial abstraction, but if we want to achieve full abstraction, then we have to use an interface.
Before going through the interface, let me share with you about Abstraction: it’s a process where you display to the user what a user wants to see and hide the unnecessary things from the outer world. And to achieve abstraction, we have to follow any of the processes by using an abstract class, and the other process is the Interface.
In this post, we will learn about an interface in Java and why we need to use an interface in the Java programming language.
What Is Interface in Java?
An interface is a collection of abstract methods that define a contract between a class and the outside world. It is a class blueprint specifying the methods the class should implement. Unlike abstract classes, interfaces cannot contain any implementation code, only method signatures.
Any class that implements an interface must provide an implementation for all of its methods. This allows Java to support multiple inheritances, where a class can inherit from multiple interfaces but can only extend a single class.
Interfaces are often used in Java to create a common API for a set of related classes, which allows them to be used interchangeably. This concept, known as polymorphism, allows for greater flexibility and reusability in code design. In this way, interfaces are an important tool for creating flexible and extensible software in Java.
Interface Syntax
interface <interface_name> { // declare constant fields // declare methods that abstract // by default. }
To create an interface, you have to use the keyword “Interface,”; and we can achieve abstraction fully with the interface. As we mentioned above, those class that wants to use the interface have to use the keyword implements and also need to provide a method body to all the mentioned methods of the interface.
Advantages of Interface
- We can achieve total abstraction by implementing interface concepts of Java programming language.
- We know that due to the 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 can extend another one.
Note: If there are two interfaces, like interface1 and interface2, and there is a class that is implemented in both interfaces, then the class should have implemented all the methods of both 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 a type of interface is called a tag or marker interface. 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 use the keyword interface to create an interface in the Java programming language.
- Because of the interface, we can get 100% abstraction; in our last post, we have seen that we can achieve partial abstraction because of the Abstract class in Java. Still, due to the interface, we are getting total abstraction because interface methods don’t have a method body.
- Interface can’t have constructors because we can not initiate an interface, and the methods don’t have a method body.
- By default, the variables 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 use the keyword “Implements.”
- If a class implements an interface, all the interface methods should be implemented; otherwise, we need to mark that class as abstract.
- An Interface cannot be declared as private, protected, or transient.
- All the Variables must be initialized at the time of declaration inside the 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, implementation is enough only once.
- A class can not implement two methods with the same name and different return types.
- 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(); } 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) { InterfaceImplementclass obj=new InterfaceImplementclass(); obj.open(); obj.close(); } }
Output:
Open Method Executed Close Method Executed
Ref: article
Leave a Reply