Java

Interface In Java

The abstract class, which is used to provide partial abstraction, was covered in the last lesson. In contrast to abstract classes, complete abstraction uses an interface. Abstraction is a technique where you “hide” unneeded information from the user and only display “useful” information (See: Abstraction). We will discuss what an interface in Java is, why we use it, and what guidelines we must adhere to while using interfaces in Java programming in this tutorial.

What is an interface in Java?

Although an interface resembles a class, it is not one. Like a class, an interface may include variables and methods, but by default, the methods specified in an interface are abstract (only method signature, no body, see: Java abstract method). Additionally, by default, the variables specified in an interface are public, static, and final. Later on in this tutorial, we will go into more depth on this.

What is the use of interface in Java?

They are used for complete abstraction, as was already indicated. Since interface methods lack a body, the class must implement them before you can access them. All of the methods of the interface must be implemented by the class that implements it. Additionally, you cannot extend more than one class in Java. However, you may implement several interfaces in a single class.

Syntax: The term “interface” is used to define interfaces. E.g.:

interface MyInterface
{
   /* All the methods are public abstract by default
    * As you see they have no body
    */
   public void method1();
   public void method2();
}

Example of an Interface in Java

A class implements an interface in this manner. It must give the body of every method specified in the interface, or you might say that the class must implement every interface method.

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();
   }
}

Output:

implementation of method1

Interface and Inheritance

An interface cannot implement another interface, as was already said. The other interface needs to be extended. See the illustration below, which shows two interfaces, Inf1 and Inf2. Since Inf2 extends Inf1, a class that implements Inf2 must also implement all of Inf2’s methods in addition to Inf1’s.

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();
    }
}

The class Demo only implements interface Inf2 in this program, but because interface Inf2 extends interface Inf1, it must also provide implementation for all of interface Inf1’s methods.

Tag or Marker interface in Java

Tag or marker interfaces are interfaces that are empty. Tag interfaces include, for instance, Serializable, EventListener, and Remote(java.rmi.Remote). These interfaces are devoid of fields and methods. To learn more, click here.

Nested interfaces

Nested interfaces are interfaces that are specified inside of other interfaces or classes. They go by the name “inner interface.” We don’t use it directly since, for instance, Entry interface in the collections framework is specified within Map interface; rather, we use it as follows: Map.Entry.

Key ideas The following are the main interfaces reminders:
1) Java does not allow us to instantiate an interface. Therefore, we are unable to build an interface object.

2) Because none of its methods have bodies, the interface offers complete abstraction. Contrarily, abstract classes provide partial abstraction since they may include both abstract and concrete (methods with bodies) methods.

Classes that want to implement an interface use the implements keyword.

4) Any method of an interface that is implemented in a class must be identified as public.

5) A class must implement every method of every interface it implements; otherwise, the class should be labeled abstract.

It is not possible to define an interface to be secret, protected, or temporary.

7) By default, all interface methods are abstract and public.

8) By default, all variables specified in an interface are public, static, and final.

interface Try
{
   int a=10;
   public int a=10;
   public static final int a=10;
   final int a=10;
   static int a=0;
}

The aforementioned sentences are all true.

9) Initializing interface variables at the moment of definition is required; otherwise, the compiler would raise an error.

interface Try
{
      int x;//Compile-time error
}

As the value of the variable x is not initialized at the time of definition, the code above will fail to compile.

10) The variables stated in the interface cannot be changed within any implementation class since they are by default public, static, and final. Here, we’re putting the interface “Try,” which contains a variable called x, into action. Because variable x is public static final by default and final variables cannot be re-initialized, when we attempted to change the value for the variable, we encountered a compilation problem.

class Sample implements Try
{
  public static void main(String args[])
  {
     x=20; //compile time error
  }
}

11) An interface can extend any other interface, but it can’t do the opposite. Interface extends and is implemented by classes.

12) Any number of interfaces may be implemented by a class.

13) Implementing a method only once is sufficient if there are two or more identical methods in two interfaces and a class implements both interfaces.

interface A
{
   public void aaa();
}
interface B
{
   public void aaa();
}
class Central implements A,B
{
   public void aaa()
   {
        //Any Code here
   }
   public static void main(String args[])
   {
        //Statements
    }
}

14) It is not possible for a class to implement two interfaces that have methods with the same name but distinct return types.

interface A
{
   public void aaa();
}
interface B
{
   public int aaa();
}

class Central implements A,B
{

   public void aaa() // error
   {
   }
   public int aaa() // error
   {
   }
   public static void main(String args[])
   {

   }
}

15) Conflicts between variable names may be addressed using the interface name.

nterface A
{
    int x=10;
}
interface B
{
    int x=100;
}
class Hello implements A,B
{
    public static void Main(String args[])
    {
       /* reference to x is ambiguous both variables are x
        * so we are using interface name to resolve the 
        * variable
        */
       System.out.println(x); 
       System.out.println(A.x);
       System.out.println(B.x);
    }
}

Advantages of interface in java:

Here are some benefits of utilizing interfaces:

  1. We can ensure the security of implementation without worrying about the implementation component.
  2. Several inheritance is not permitted in Java, but you can use interfaces instead since you may create multiple interfaces.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button