An interface is a construct which contains the collection of purely undefined methods or an interface is a collection of purely abstract methods.
Interface <interface name> { Variable declaration; Method declaration; }
Interface is a keyword which is used for developing user defined data types. Interface name represent a JAVA valid variable name and it is treated as name of the interface. With respect to interface we cannot create an object directly but we can create indirectly.
Variable declaration represents the type of data members which we use a part of interface. Whatever the variables we use in the interface are meant for general purpose (the variables like PI, e, etc.).
Whatever the variables we write in the interface, they are by default belongs to:
public static final xxx data members
xxx represents data type, variable name and variable value.
All variables must be initialized (otherwise it will be compilation error). Method declaration represents the type of methods we use as a part of interface. All the methods of interface are undefined methods and to make those methods as abstract, the JAVA programmer need not to write a keyword abstract explicitly before the declaration of interface method.
Since all the methods of interface are meant for general purpose hence they must give universal access. To make it as universal access the JAVA programmer need not to write a keyword public explicitly before the method declaration. Hence, by default all the methods of interfaces belong to public abstract methods.
For example:
Interface i1 { Int a; //invalid since, initializing is mandatory Int b=20; Void f1 (); Void f2 (); }
Note:
Whenever we compile an interface, we get <interface name>.class as an intermediate file, if no errors are present in interface.
Syntax-1 for reusing the features of interface(s) to class:
[abstract] class <clsname> implements <intf 1>,<intf 2>.........<intf n> { variable declaration; method definition or declaration; };
In the above syntax clsname represents name of the class which is inheriting the features from 'n' number of interfaces. 'Implements' is a keyword which is used to inherit the features of interface(s) to a derived class.
Note:
When we inherit 'n' number of abstract methods from 'n' number of interfaces to the derived class, if the derived class provides definition for all 'n' number of abstract methods then the derived class is known as concrete class. If the derived class is not providing definition for at least one abstract class and it must be made as abstract by using a keyword abstract.
Syntax-2 inheriting 'n'number of interfaces to another interface:
interface <intf 0 name> extends <intf 1>,<intf 2>.........<intf n> { variable declaration cum initialization; method declaration; };
For example:
interface I1 { int a=10; void f1 (); }; interface I2 extends I1 { int b=20; void f2 (); };
If one interface is taking the features of another interface then that inheritance is known as interface inheritance
Syntax-3:
[abstract] class <derived class name> extends <base class name> implements <intf 1>,<intf 2>.........<intf n> { variable declaration; method definition or declaration; };
Whenever we use both extends and implements keywords as a part of JAVA program we must always write extends keyword first and latter we must use implements keyword.
Important points: