Interface In Java

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, 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 what a user wants to see and hide the unnecessary things from the outer world. To achieve abstraction, we have to follow any of the processes using an abstract class, and the Interface is the other process.

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 full abstraction with the interface. As we mentioned above, those classes that want to use the interface have to use the keyword implements and also need to provide a method body for all the mentioned interface methods.

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

package com.softwaretestingo.interfacepackage;
interface MyInterface
{
	/* compiler will treat them as:
	 * public abstract void method1();
	 * public abstract void method2();
	 */
	public void method1();
	public void method2();
}
public class InterfaceDemoEx 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[] args) 
	{
		MyInterface obj = new InterfaceDemoEx();
		obj.method1();
	}
}

Output:

implementation of 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

package com.softwaretestingo.interfacepackage;
interface Interface1
{
	public void method1();
}
interface Interface2 extends Interface1 
{
	public void method2();
}
public class InterfaceExtendEx implements Interface2
{
	/* Even though this class is only implementing the
	 * interface Interface2, it has to implement all the methods
	 * of Interface1 as well because the interface Interface2 extends Interface1
	 */
	public void method1()
	{
		System.out.println("method1");
	}
	public void method2()
	{
		System.out.println("method2");
	}
	public static void main(String args[])
	{
		Interface2 obj = new InterfaceExtendEx();
		obj.method2();
	}
}

Output:

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. Examples of marker interfaces are 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.

Conclusion:

In this guide, we discussed a very important concept in Java programming called interfaces. We explained what interfaces are and talked about their advantages and disadvantages. We also showed how to create and use interfaces using easy examples, including multiple inheritances and interface inheritance. We discussed how interfaces differ from abstract classes and covered topics like default and static methods, naming conventions, and functional interfaces.

Interfaces are simple structures with a clear purpose but very powerful. They can make code easier to read and understand. So whenever you have the chance, using interfaces in your programming is a good idea.

Ref: article

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment