Can declare and implement variables. However, "public static final" is implicitly added.
interface noTsukaikata {
int Num = 6; //Since final is attached, it cannot be initialized. → It is treated as a constant.
}
Only abstract methods can be declared.
interface noTsukaikata {
void method(); //Declaration of abstract method (method in interface is given abstract clause)
// abstract void method();Is automatically converted to.
}
In fact, it can be implemented by using the default clause or static clause.
interface deJisou{
//Implement methods in interface by using default clause
default void method1(){
//more codes here
}
//Implement methods in interface by using static clause
static void method2(){
//more codes here
}
//Compile error without default or static clause
void method3() {
//more codes here
}
}
interface can only inherit implements.
interface Super{
int Num;
default void method1(){
//more codes here.
}
static void method2(){
//more codes here.
}
void method3();
}
class Sub implements Super{
//The variable is final so you don't have to write it.
//Since the default method has already been implemented, it is not necessary to implement it in the Sub class. Can be overridden as needed.
//Since static methods are not inherited, they cannot be implemented in Sub class.
//Implement method3
@Override
protected void method3(){//Disclosure range(scope)Access modifiers may be changed as long as is not narrowed.
//more codes here.
}
}
The contents of the Sub class by inheriting the above are as follows.
class Sub implements Super{
int Num = 6;
default void method1(){
//more codes here.
}
//There is no static method because it is not inherited.
protected void method3(){
//more codes here.
}
}
Treated exactly the same as a regular class.
abstract class Super {
int Num1; //Can be declared
int Num2 = 6; //Can be initialized
abstract int Num3 = 10; //Compile error (variables cannot be abstracted)
}
Abstract methods can also be declared.
interface Super {
abstract void method1(); //You can declare abstract methods.
void method2() {} //It can be implemented without the abstract clause.
}
abstract can only inherit extends.
abstract class Super {
int Num1;
int Num2 = 6;
abstract void method1();
void method2() {}
}
class Sub extends Super{
@Override
void method1() {}
}
The contents of the Sub class by inheriting the above are as follows.
class Sub extends Super{
int Num1;
int Num2 = 6;
void method1() {}
void method2() {}
}
interface | abstract | |
---|---|---|
variable | Automatically "public static final" | Exactly the same as the regular class |
Method | 基本的に抽象Methodのみ宣言できる。ただし、「default」や「static」句を利用し、実装できる。 | 抽象Methodも宣言できる。 |
Inheritance method | Inherited by implements. Subclasses implement all abstract methods. | Inherited by extends. Subclasses implement all abstract methods. |
implements ・ ・ ・ Inherits interface. extends ・ ・ ・ Inherits the class.
Conclusion,
Recommended Posts