Interface in Java

Interface in Java

Table of Contents

In contrast to abstract classes, full abstraction uses an interface. In the abstraction process, you “hide” unneeded information from the user and only display “relevant” information about an item Interface in Java . We will discuss what an interface in Java is, why we use it, and what guidelines we must adhere to when using interfaces in Java programming in this guide. Check out the online Java training to learn more about how Interface in Java works.

What is an Interface in Java?

Although an interface resembles a class, it is not one. Similar to a class, an interface can have variables and methods, but by default, the methods stated in an interface are abstract (they just have a method signature and no body; for more information, see a Java abstract method). Additionally, by default, the variables declared in an interface are public, static, and final.

What is the use of the interface in Java?

They are used for full 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 can implement several interfaces in a single class.

Examples of Interface in Java

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

Interface in Java
  1. Interface and Inheritance

An interface cannot implement another interface, as was already mentioned. The other interface needs to be extended. For instance, suppose we had the Inf1 and Inf2 interfaces. Since Inf2 extends Inf1, a class that implements Inf2 must also implement all of Inf2’s methods in addition to Inf1’s.

  1. 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. 

  1. 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 because, for instance, the Entry interface in the collections framework is declared inside the Map interface; rather, we use it as follows: Map. Entry.

The following are the main interface reminders:

  • Java does not allow us to instantiate an interface. Therefore, we are unable to create an interface object.
  • Because none of its methods have bodies, the interface offers complete abstraction. Contrarily, abstract classes offer partial abstraction because they can contain both abstract and concrete (methods with bodies) methods.
  • Classes can implement an interface by using the implements keyword.
  • Any method of an interface that is implemented in a class must be identified as public.
  • A class must implement every method of every interface it implements; otherwise, the class should be labelled abstract.
  • It is not possible to declare an interface to be private, protected, or temporary.
  • By default, all interface methods are abstract and public.
  • By default, all variables declared in an interface are public, static, and final.
  • Initializing interface variables at the moment of definition is required; otherwise, the compiler would throw an error.
  • The variables stated in the interface cannot be changed inside any implementation class because they are by default public, static, and final. Here, we are putting the “Try” interface, which has a variable x, into action. Because variable x is public static final by default and final variables cannot be re-initialized, when we attempted to set the value for the variable, we encountered a compilation problem.
  • An interface can extend any other interface, but it can’t do the opposite. The interface extends and is implemented by classes.
  • Any number of interfaces may be implemented by a class.
  • Implementing a method just once is sufficient if there are two or more identical methods in two interfaces and a class implements both interfaces.
  • Two interfaces having methods with the same name but distinct return types cannot be implemented by the same class.
  • Conflicts between variable names can be addressed using the interface name.
Interface in Java

Advantages of Interface in Java

Here are some benefits of using interfaces:

  • We can ensure the security of implementation without worrying about the implementation component.
  • Some inheritance is not permitted in Java, but you can use interfaces instead because you can create several interfaces.

Conclusion

 To learn more about Interface in Java and its benefits, you can check our free Java course online.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class