Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

Interface in Java With Example

Interface are type of reference in Java. It’s similar to class. It is an abstract collection of techniques. The interface of Java is a means to create abstraction. It is possible to abstract only methods within Java’s Java interface, but not the methods body. This is utilized to create abstractness and multi-generation in Java.

If an object implements an interface but does not include method bodies for every function that are specified in the interface the class should declare itself abstract. Interface comparable to writing an entire class. A class, however, describes the attributes and the behaviors associated with an object. The interface also contains the actions that a class can implement.

An interface could comprise any number of ways. An interface is created in a text file with an .java extension, and an interface’s name being what the extension is called. The byte code for an interface is stored in the .class file.

Interface are included in packages and the bytecode file must be located in the same directory structure as the name of the package.

Syntax :

Interface {

// declare constant fields

// define methods that abstract

/by default.

public void method1();

public void method2();

}

Why should you use the Java interface?

  • There are three primary reasons to utilize interface. These are listed below.
  • Since interface methods don’t have bodies that is why they need to be designed by the class in order that you are able to access them.
  • A class which implements the the interface must implement all methods that are part of the interface.
  • The Java programming language doesn’t permit the extension of multiple classes. however, you can create more than one interface in your class.
  • It’s used to create abstraction.
  • Through interfaces, we are able to facilitate the operation for multiple inheritance.
  • It is a method to create loose coupling.
  • It is not possible to instantiate an interface.
  • The methods used in the interface have abstract concepts. The interface can’t contain instances of fields. Fields that are able to be included in an interface should be declared as permanent and definitive.
  • An interface is not a source of any constructors.

An interface isn’t expanded by classes. It is created by an existing class. An interface may extend several interfaces.

Do you have a tag for interface?

A interface without a member is referred to as a marker interface, for instance, Remote, Serializable, Cloneable and more. They provide an essential data source to the JVM to ensure that the JVM can perform a useful action.

Implementing Interfaces

If the class implements an interface You can imagine the class as a contracting party, accepting to execute the particular actions that the interface performs. If a class is unable to fulfill all the actions that are required by the interface it has to declare itself abstract.

A class makes use of the keyword implements to create an interface. The word implements appears in the declaration of the class following the extends section in the declaration.

Interface in Java using example programs

Abstract class that can be used to achieve partial abstraction. In contrast to abstract classes, an interface is utilized to complete abstraction. Abstraction is the process by which you only display “relevant” data and “hide” the details that are not relevant to something from user. Here concept of an interface in Java and the reasons we utilize it, and the guidelines we need to follow when working with Interfaces within Java Programming.

How do I define an interface for Java?

Interface appears to be an object, but it’s not something that is a class. An interface may have variables and methods similar to classes, but the methods that are declared in the interface are, by default, abstract (only signatures of methods, not a body, refer to: Java abstract method). Additionally the variables declared by an interface are declared public, static, and definitive by default. We’ll discuss this further in this article.

What’s the purpose of an interfaces in Java?

As previously mentioned, they can be used to abstract the entire system. Since interfaces’ methods are not body-based and are not a part of the body, they need to implement by the class prior to you are able to access them. A class which implements an the interface must implement all methods associated with that interface. Additionally, the Java programming language doesn’t allow the extension of more than one class. however, you are able to implement more than one interface in your class.

Syntax:

Interfaces are declared using the keyword “interface”. E.g. :

MyInterface interface

{

public void method1();

public void method2();

}

Example of an Interface in Java

This is the way an interface is implemented by a class. It must provide the body of all methods declared in the interface, or, in other words, it must implement all interface methods.

Are you aware? The class implements interfaces, however an interface extends an interface.

interface MyInterface

{

The compiler will consider them to be:

public void method1();

public void method2();

}

Class Demo implements MyInterface

{Otherwise, you’ll get compilation error

public void method1()

{

System.out.println(“implementation of method1”);

}

public void method2()

{

System.out.println(“implementation of method2”);

}

public static public static main(String arg[“arg”)

{

MyInterface obj = new Demo();

obj.method1();

}

}

Output:

Method 1 implementation

You might also want to know: the difference between interface and abstract class

Interface and Inheritance

As mentioned above the interface cannot create a new interface. It must extend the existing interface. Take a look at the below example, in which we have two interfaces , Inf1 and Inf2. Inf2 extends the Inf1 interface, so if a class implements Inf2, it must implement all interface methods Inf2 as well as inf1.

Find out More about inheritance: Java Inheritance

interface Inf1{

public void method1();

}

{interface Inf2 extends Inf1 Inf2 interface extends Inf1’s

public void method2();

}

Public Class Demo implements Inf2{

This class only implements the

public void method1(){

System.out.println(“method1”);

}

public void method2(){

System.out.println(“method2”);

}

public static public static main(String primaryArgs []){

Inf2 obj = new Demo();

obj.method2();

}

}

In this program Demo, it is the case that Demo class Demo only implements the interface Inf2, however , it is required to implement the interface’s methods in Inf1, too, since Inf2’s interface extends Inf1.

Tag interface in Java

An empty interface is also known as a marker or tag interface. For example Serializable, EventListener, Remote(java.rmi.Remote) are tag interfaces. Interfaces in Java do not contain any fields and methods inside it.

Nested interfaces

A class or interface that is declared in a different interface or class is known as the nested interface. These are also referred to as an inner interface. For instance, the Entry interface of Collections Framework is declared within Map interface. That’s the reason we don’t utilize it directly, instead we make use of it .

Here are essential points to be aware of when it comes to interfaces:

1.) We aren’t able to instantiate an interface using Java. This means that we are unable to make the object of an interface.

2.) Interface provides full abstraction because all of its methods do not have body. However, abstract class offers partial abstraction, as it is able to contain abstract and concrete(methods that have body) methods.

3.) The implement keyword can be utilized by classes to create an interface.

4.) In the event of providing an implementation in the class of interface, it must be referred to as public.

5.) The class that implements any interface has to implement all methods that are associated with the interface, or else the class must be declared to be abstract.

6.) Interface cannot be declared as private, secure or temporary.

7) All interface methods are, by default, open and transparent.

8.) Interface variables should be declared during declaration, otherwise the compiler will issue an error.

interface, Try

{

int x;

}

Advantage of using interfaces

  • In the absence of worrying about the implementation aspect we can ensure the peace of mind of the implementation
  • In Java it is not permitted, but you are able to implement interfaces to implement it since you can create multiple interfaces.
  • It’s used to achieve absolute abstraction.
  • Java does not allow multiple inheritance within the context of classes, however, with the help of interfaces, it is possible to be able to achieve multiple inheritance.
  • Interface is not declared private, protected , or temporary.
  • All interface methods are abstract and open to public.
  • Variables that are declared in interface are public, static , and definitive by default.
  • It can also be used to create loose coupling.
  • Interfaces are used to create abstraction.

Java Interface Example

In this case the Printable interface is only one method and the implementation of it is found in the class A3.

{interface printableInterface printable

void print();

}

public void print(){System.out.println(“Interface example”);}

public static public static main(String str) args []){

A3 obj = new A3();

obj.print();

}

}

Output:

Interface example

Extending Multiple Interfaces

  • A Java class is only able to be extended by one parent.
  • A class can implement any number of interfaces.
  • Multiple inheritance is not permitted.
  • Interfaces are not classes however an interface may extend over more than an interface that is its parent.
  • An interface can extend any interface but cannot implement it.
  • Class implements interface and interface extends interface.
  • The extends keyword can be used only once, and parent interfaces are specified as a comma-separated language
Published
Categorized as Java

Leave a comment

Your email address will not be published.