.
Java Tutorial: Interface in Java
Interface
- It looks like a classNamebut it won’t contain any function definition
- It holds constants and static methods
- It holds only the skeleton of what a classNameis going to implement
- The functions prototype in an interface has to be implemented in a classNamecompletely
- It holds n number of functions
- An object is not created for an interface
- Constructor is impossible inside an interface
- Like classNameinheritance interface inheritance is allowed
- An interface supports code reusability by extending an base interface
- For Interface inheritance extends keyword is employed
- For the implementation of an interface in a classNameimplements keyword is applied
Interface Declaration Syntax
- The keyword interface is employed to announce an interface
- It is abstract by default
- The function prototype in an interface is abstract by default
- Those functions are public by default
Syntax for declaration
public interface name_of_an_interfac1 { //static / final variables ; //method prototype declaration; }
Syntax for extending an interface with its implementation in a class
public interface name_of_an_interfac1 { //static / final variables ; //method prototype declaration; } public interface name_of_an_interfac2 extends name_of_an_interfac1 { //static / final variables ; //method prototype declaration; } classNamename_of_a_clas implements name_of_an_interfac1, name_of_an_interfac2 { // interface method prototype definition }
Example
The beneath example show how an interface animal is implemented in a classNameMammal
.