Similar to class, interface in Java is a collection of abstract methods. Interfaces are implemented by class so it inherits the abstract methods. Interface might contain nested types, default and static methods and constants. Only default and static methods will contain the method bodies in interface. The coding process of interface is similar to the coding process of a class. But methods present in interface must be defined in class.
Expect abstract class, all the other methods used in interface must be defined in the class.
Similarities between interface and class:
- Any number of methods can be used in an interface.
- The name of file which stores the interface is similar to the interface name. Interface files are stored with .java extension.
- .class file will contain the byte code of an interface.
- Interfaces usually appear in packages, the directory structure of bytecode is identical to the interface package name.
Difference between Interface and Class
- An interface cannot be instantiated like a class.
- Unlike class, constructors are not used in interface.
- All the methods used in an interface are abstract.
- Interface contains instance fields, only fields which present in interface can be declared as static and final.
- A class cannot extend an interface while it can implement one.
- Classes cannot be extended while an interface can be extended into several interfaces.
How to Declare an Interface?
A keyword, “interface” is used to declare an interface. The program given below is an example for an interface.
/* Name of this Program: InterfaceOne.java */
import java.lang.*;
//Here comes the import statements
public interface InterfaceOne
{
//You can add Many number of abstract function declarations
}
Properties of an interface:
- All the interfaces are abstract.
- All the methods present in interfaces are also abstract.
- Methods present in interfaces are essentially public.
How to implement an interface?
Implementing an interface is like signing an agreement to perform specific instructions. If a class fails to perform these specific instructions then it should be declared as an abstract class. The keyword “Implements” is used by class to implement an interface.
Rules to follow while implementing an interface
- More than one interface can be implemented by a class.
- Many interfaces can be implemented but only one class can be extended.
- An interface can be extended to another interface.
The following rules must be eyeballed while overriding methods present in an interface.
- Exceptions which are checked should not be declared in an interface.
- The method signature and return type should be maintained while method overriding.
- Implementation of interface methods is not required.
How to Extend an Interface?
Similarly to class, Interface can be extended into another interface. The keyword “extends” is used to extend an interface. The methods of parent interface can be inherited by child interface.
- Many: Java interview Q&A available here
In the following program, Sports interface is extended by Cricket and Football interfaces.
//Program Name: SportsProgram.java
public interface SportsProgram
{
public void MyLocalTeam(String name);
public void MyOtherTeam(String name);
}
//Program Name: FootballExample.java
public interface FootballExample extends SportsProgram
{
public void LocalTeamScore(int numbers);
public void OtherTeamScore(int numbers);
public void LastQuarter(int Quart);
}
In the above program, there are four methods in “Cricket” interface but two out of these four methods are inherited from “Sports” interface. So a class which implements the interface “Cricket” requires to implement all the six methods. A class which implements “Football” require to interface five methods.
Multiple inheritance is not allowed in Java. However an interface can extend more than one parent interface using the keyword “extends”. To extend an interface to several parent interface, the extends is followed by several parent interfaces which are separated by commas.