Continuing from previous topic. Interface
In this post, discussion will be on the interface body.
Lets go inside interface body !!
Interface body can contain following components:
1. constants
2. abstract methods,
3. class
4. interface
From Java 8,
5. default methods, and
6. static methods.
In Java interface,
- All the components constant, abstract method, class, interface, default & static method will have public scope implicitly.
- All the components other than abstract method & default method will have static modifier implicitly.
- Default methods are defined with the default modifier, and static methods with the static keyword.
- All constant values defined in an interface are implicitly public, static and final.
- All the nested interface are implicitly public, static
- All the nested class are implicitly public, static
Interface Example:
public interface Interface1 {
int constant1 = 2; // constant
void getMethod(); // abstract method
class Class1 { // class ( static class )
String getSum(int a, int b){
int sum = a + b;
return " inner static class call, sum:" + sum;
}
}
interface innerInterface {
// static interface
}
// default method
default String defaultMethod1() {
return " interface1. defaultMethod1 ";
}
// default method
default String defaultMethod2() {
return " interface1. defaultMethod2 ";
}
// static method
static String staticMethod1() {
return " interface1. staticMethod1 ";
}
// static method
static String staticMethod2() {
return " interface1. staticMethod2 ";
}
}


Leave a reply to nadiya Cancel reply