Creation of an interface should include
An example of an interface follows:
interface ImplementMe{
final int noLines = 10;
public void showData();
}
//===================================//
/*Show that interfaces can be inherited as well. The extended subclass is also an interface and is not bound by the rule that says the implementation must be supplied if an interface is implemented.*/
interface ImplementMeToo extends ImplementMe{
public void showMoreData();
//ImplementMeToo also has a method called showData from its superclass, and both must be included in a program that implements the interface ImplementMeToo//
}
Using an interface reference and variables should be done within the context of a program which implements the interface, extends another class, and utilizes the value of one of the variables. An example follows:
class Demonstration extends Object{
//override the toString method that is inherited from the class Object
public String toString() {
return "our own version of toString";
}
//create a new method that is not inherited from the class object
public String getAnotherString() {
return "this is from the method getAnotherString in Demonstration";
}
}
//The following class implements, inherits and prints the value of the final variable
//from the interface.
class AnotherDemonstration extends Demonstration implements ImplementMeToo{
public void showData() {
System.out.println("I don't have any data to show!");
}
public void showMoreData() {
//Nothing even has to be here, as long as the curly braces are supplied
}
public void printTheFinal( ){
System.out.println("The value of noLines is " + noLines);
}
}