I. Purpose of using interfaces (interface)
Java only supports single inheritance, that is, a class can inherit at most one direct parent, and the main function of the interface is the ability to implement multiple inheritance similar to a class.
Two. Nature of the interface
1. The interface has inheritance, that is, the sub-interface inherits the properties and methods of the parent interface. And you don't want a class to inherit only one parent class, an interface can inherit multiple parent interfaces.
2. The methods in the interface are abstract methods.
3. To implement an interface for a class, this class implements an abstract method that is not implemented in the interface.
Example:
PackageL1105;Interfacetestimplement { Public voidSun ();//defining an abstract method in an interface }classRiverImplementstestimplement{//declare the interface that this class implements by using the Implements keyword in the class intA; PublicRiverinta) { This. a=A; System.out.println ("Hello today is" +a); } Public voidSun () {//implementing methods that are not implemented in an interface in a classSystem.out.println ("This is a dog"); } }classmoon{ Public Static voidMain (String args[]) {testimplement im1=NewRiver (10); Im1.sun (); }}examples of use of interface
Interfaces in Java