The general form of an interface is like class as shown below.
modifiers interface <interfac_name>
{ modifiers return_type method1(parameter-list);
modifiers return_type method2(parameter-list);
.
modifiers type final_var1 = value1;
modifiers type final_var2 = value2;
.
}
1. Modifiers for Top-Level Interface
The only modifiers that can be used with the top-level interfaces are abstract and public.
Even if we do not use abstract modifier, the interface is implicitly declared to be abstract so use of modifier abstract is redundant.
The visibility of a top-level interface can be either package or public just like top-level class. If no visibility modifier is used then the visibility of the interface is assumed to be package.
2. Interface Methods
Methods declared in an interface have no body. They end with a semicolon after the parameter list. They are, essentially, abstract methods and there can be no default implementation of any method specified within an interface.
All the methods declared in an interface have two modifiers, which are implicit:
abstract public method(parameter-list);
Methods are always abstract, which means they do not have any body and have public access.
All the methods declared in an interface are instance methods to be defined in the sub-classes.
We cannot declare static methods in interfaces.
3. Interface Data Members
We can define only constants in the interfaces. All the variables in an interface are implicitly public, final and static meaning they cannot be changed by the implementing class. They must also be initialized with constant value.
Variables in an interface have implicit modifiers as shown below:
public static final int x = 10;
The variable x is public, static and final. It means it is a static variable with public access. The variable is also declared final, which means that it must be assigned a value at the time of declaration, which cannot be modified later on. The keyword final is like const in C/C++.
Example: The following program defines an interface for a stack i.e. it abstracts the public part of the stack.
interface StackInterface
{ void push(int x);
int pop();
}
The interface specifies that the stack has just two operations and any class sub-classing (implementing) this interface must provide bodies of push() and pop() methods.
modifiers interface <interfac_name>
{ modifiers return_type method1(parameter-list);
modifiers return_type method2(parameter-list);
.
modifiers type final_var1 = value1;
modifiers type final_var2 = value2;
.
}
1. Modifiers for Top-Level Interface
The only modifiers that can be used with the top-level interfaces are abstract and public.
Even if we do not use abstract modifier, the interface is implicitly declared to be abstract so use of modifier abstract is redundant.
The visibility of a top-level interface can be either package or public just like top-level class. If no visibility modifier is used then the visibility of the interface is assumed to be package.
2. Interface Methods
Methods declared in an interface have no body. They end with a semicolon after the parameter list. They are, essentially, abstract methods and there can be no default implementation of any method specified within an interface.
All the methods declared in an interface have two modifiers, which are implicit:
abstract public method(parameter-list);
Methods are always abstract, which means they do not have any body and have public access.
All the methods declared in an interface are instance methods to be defined in the sub-classes.
We cannot declare static methods in interfaces.
3. Interface Data Members
We can define only constants in the interfaces. All the variables in an interface are implicitly public, final and static meaning they cannot be changed by the implementing class. They must also be initialized with constant value.
Variables in an interface have implicit modifiers as shown below:
public static final int x = 10;
The variable x is public, static and final. It means it is a static variable with public access. The variable is also declared final, which means that it must be assigned a value at the time of declaration, which cannot be modified later on. The keyword final is like const in C/C++.
Example: The following program defines an interface for a stack i.e. it abstracts the public part of the stack.
interface StackInterface
{ void push(int x);
int pop();
}
The interface specifies that the stack has just two operations and any class sub-classing (implementing) this interface must provide bodies of push() and pop() methods.