Java Interfaces

Java  Interfaces

An interface is a reference type in Java. It’s similar to the class. It is a collection of abstract methods. A class implements an interface that inherits abstract methods from the interface.

Using abstract methods, an interface can also contain constant standard methods, static methods, and nested types. The Method bodies exist only for default methods and static methods.

Write an interface similar to writing a class. But a class describes the properties and behavior of an object. Unless the class that implements the interface is abstract, all interface methods must be defined in the class.

An interface is similar to a class of the following types –

  • An interface is stored in a file with the extension .java with the name corresponding to the filename interface.
  • The interfaces are in packets and bytecode file that must be in a directory structure that matches the package name.
  • However, an interface that is different from a class of several options, including
  • You can instantiate an interface.
  • An interface does not contain a constructor.
  • All methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that appear in an interface must be declared static and final.
  • An interface does not extend a class; It is implemented by a class.
  • An interface can extend various interfaces.

Declaring Interfaces

The keyword of the interface is used to declare an interface. Here is a simple example to declare an interface –

Example

Next, an example of an interface -

/ * File name: NameOfInterface.java * /

import java.lang. *;

// Any number of import declarations

public interface NameOfInterface {

// Any number of final static fields

// Any number of abstract method declarations \

}

Interfaces has the following properties –

  • An interface is implicitly abstract. You do not need to use the abstractkeyword while declaring an interface.
  • Each method in an interface is also implicitly abstract, so the abstract keyword is not necessary.
  • The methods in an interface are implicitly public.

Example

/ * File name: Animal.java * /

animal of the interface {

public void eat ();

public empty trip ();

}

Implementation of Interfaces

When a class implements an interface, you can think of the class as signing a contract, in agreement to execute the specific behaviors of the interface. If a class does not execute all the behaviors of the interface, the class must be declared as abstract.

A class uses the keyword implements to implement an interface. The keyword implements appears in the class declaration after the extension part of the declaration.

Example

/ * File name: MammalInt.java * /

The public class MammalInt implements Animal {

public void eat () {noun

System.out.println ("Mammal eats");

}

System.out.println ("Mammal travels");

}

public int noOfLegs () {

return 0;

}

public static void main (string args []) {

MammalInt m = new MammalInt ();

meat ();

m.travel ();

}

}

This will produce the following result –

Output

Mammal eats

Mammal travels

When substituting methods defined in interfaces, there are several rules to follow –

  • Proven exceptions should not be declared in implementation methods other than those declared by the interface method or subclasses of those declared by the interface method.
  • The signature of the interface method and the same type or subtype of return must be maintained when replacing the methods.
  • An implementation class itself can be abstract and, if so, the interface methods do not need to be implemented.

When the implementation interfaces, there are several rules –

  • A class can implement more than one interface at a time.
  • A class can extend only one class, but implement many interfaces.
  • An interface can extend another interface, similar to what a class can extend another class.

Extending Interfaces

An interface can extend another interface in the same way that one class can extend another class. The keyword is used to extend an interface and the daughter interface inherits the main interface methods.

The next sports interface is extended by hockey and soccer interfaces.

example

// File name: Sports.java

the public interface sports {

public void setHomeTeam (name of the string);

public void setVisitingTeam (name of the string);

}

// File name: Football.java

the public interface of football extends sports {

public void homeTeamScored (int points);

public void visitTeamScored (int points);

public void endOfQuarter (int quarter);

}

// File name: Hockey.java

The public interface Hockey extends sports {

public void homeGoalScored ();

public void visitGoalScored ();

public void endOfPeriod (int period);

public void overtimePeriod (int ot);

}

The Hockey interface has four methods, but it inherits two from Sports; Thus, a class that implements hockey needs to implement all six methods. In the same way, a class that implements football needs to define the three methods of soccer and the two methods of sport.

Extending Multiple Interfaces

A Java class can only extend a primary class. Multiple inheritance is not allowed. Interfaces are not classes, however, and an interface can extend more than one main interface.

The keyword is used once and the primary interfaces are declared in a comma-separated list.

For example, if the Hockey interface extended sports and events, it would be declared as –

Example

public interface Hockey extends Sports, Event

Interested in learning Java? Enroll now:” Java training in Chennai “

Tagging Interfaces

The most common use of extension interfaces occurs when the main interface does not contain any methods. For example, the MouseListener interface in the java.awt.event package extended java.util.EventListener, which is defined as –

example

java.util package;

EventListener public interface

{}

An interface without methods is called a marking interface. There are two basic purposes of designing label interfaces –

Create a common parent – As in the EventListener interface, which spans dozens of other interfaces in the Java API, it is possible to use a dial interface to create a common parent among a group of interfaces. For example, when an interface extends EventListener, JVM knows that this specific interface will be used in an event delegation scenario.

Add a data type to a class – This situation is where the marking term comes from. A class that implements a marking interface does not need to define any method (since the interface has none), but the class becomes an interface type through polymorphism.

 

To getting expert-level training for Java Training in your location – java training in Chennai | java training in Bangalore  | java training in Pune | java training in Chennai | java training in Bangalore | java training in Chennai | java interview questions and answers |  core java interview questions and answers | java training in Chennai | For getting java online training | java online training