Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

Interface in Java

An interface in Java is a reference type. It is similar in concept to class. It’s a collection abstract methods. A class implements an interface and inherits the abstract methods. Interfaces in Java are a way to achieve abstraction. The Java interface can only contain abstract methods, and not the method body. It is used in Java to achieve multiple inheritance and abstraction.

It can’t be instantiated like an abstract class. An interface can also include constants, default, static, and nested methods. Method bodies are only available for static and default methods.

An interface is the same as a class. A class describes the attributes or behaviors of an object. An interface is a collection of behaviors that a class implements.

Syntax :

Interface {

// declare constant fields

// Declare abstract methods

// By default

|}

If the interface implementation class is not abstract, then all interface methods must be described in the class. In the following ways, an interface is like a class: A variety of methods can be included in an interface.

What is Use of Interface in Java?

An interface is written in an interface file that has a.java extension. The name of the interface must match the file’s name.

You cannot create or instantiate an interface object, just like a class. All methods within an interface must be declared abstract. An interface doesn’t contain constructors; however, a class does.

Instance fields cannot be included in an interface. An interface can only contain fields that have been declared as final and static. A class cannot extend or inherit an interface; it implements the interface.

An interface can’t implement any class. class file contains the byte code for an interface. Interfaces are found in packages. The bytecode files must be located in a directory structure matching the package name.

Use of Interface in Java

  • An interface can be different from a class in many ways.
  • An interface cannot be instantiated.
  • An interface doesn’t contain constructors.
  • All methods within an interface are abstract.
  • Instance fields cannot be included in an interface. An interface cannot contain instance fields.
  • An interface is not extended or implemented by a particular class.
  • Multiple interfaces can be extended by an interface.

Why do We Use Interface in Java?

  • It is used for total abstraction.
  • Java does not support multiple inheritance for classes, however interfaces can allow multiple inheritance.
  • It can also be used to create loose coupling.
  • It is used for abstraction.
  • Interface allows us to support multiple inheritance functionality.
  • You can use it to create loose coupling.
  • Interfaces are used for abstraction implementation. Interfaces are used to implement abstraction.

Declaring Interfaces

An interface is declared using the interface keyword. This is an example of how to declare an interface.

Exemple

/* File name : NameOfInterface.java */

Java.lang. *

// Any number import statements

{public interface NameOfInterface NameOfInterface public interface

// Any number of static final fields

|}

Extension Interfaces

A class can also extend an interface. To extend an interface, the extends keyword can be used. The child interface inherits all the methods of its parent interface.

Multiple Interfaces in Java

A Java class cannot extend more than one parent class. Multiple inheritances are not possible with Java classes. Interfaces are similar Java classes, but can be extended to more than one parent interface.

Tagging Interfaces

When the parent interface doesn’t contain any methods, this is when extending interfaces are most commonly used. For example, the MouseListener interface in the java.awt.event package extended java.util.EventListener, which is defined as –

Exemple:

package java.util;

EventListener Public Interface

A tagging interface is an interface that has no methods. Tagging interfaces have two main design goals:

You can create a common child – Similar to the EventListener interface which is extended by many other interfaces in Java API, you may use a tag interface to create a common child among several interfaces. The JVM can recognize that an interface extends EventListener and will use it in an event delegation scenario.

A data type that is added to a class. This is what the term “tagging” comes from. Because the interface doesn’t have methods, a class that implements a tag interface does not need any methods to be defined. However, the class is made an interface type by polymorphism.

Nested Interfaces

An interface declared within another interface or class can be called a “nested interface” or an “internal interface”.

The Nested Interface can’t be accessed directly. To resolve the namespace, we mainly use a Nested Interface by grouping similar interfaces or interfaces and classes together.

The name of the outer interface or outer class is followed by a dot (. The interface name is used to call the nested interfacing.

What is a marker or tagged interface?

A marker, or tagged interface is an interface that does not have members. This interface can be Serializable Cloneable Remote, Serializable Cloneable, Cloneable, etc. These are essential information that the JVM needs to perform a useful operation.

//How Serializable Interface is Written?

{public interface SerializablePublic interface Serializable

After using the keyword extends, multiple parent interfaces can be declared in a separated list.

Published
Categorized as Java

Leave a comment

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