- interface
In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Extension is discussed later in this lesson.
The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation). Default methods are defined with the default
modifier, and static methods with the static
keyword. All abstract, default, and static methods in an interface are implicitly public
, so you can omit the public
modifier.
抽象方法
- static method
A class does not inheritstatic
methods from its superinterfaces.
- 接口可以实现多继承
public interface GroupedInterface extends Interface1, Interface2, Interface3 {
// constant declarations
// base of natural logarithms
double E = 2.718282;
// method signatures
void doSomething (int i, double x);
int doSomethingElse(String s);
}
接口继承
constants,
method signatures,
default methods,
static methods,
and nested types.
哪些是可以被继承的?Default methods and abstract methods in interfaces are inherited like instance methods.
constants,
abstract method,
default methods继承
What You Can Do in a Subclass
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
The inherited fields can be used directly, just like any other fields.
You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
You can declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
You can declare new methods in the subclass that are not in the superclass.
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.在接口继承和实现中, 父接口的static method 不可被子类继承, 但可以被同名同参的静态方法覆写或隐藏
you can keep static methods specific to an interface in the same interface rather than in a separate class.
public interface IMy {
// constant filed
String str = "str";
// abstract method
void showYourMind();
// static method
static void loveForever(String[] args) {
System.out.println("args = [" + Arrays.toString(args) + "]");
}
// nested type
class MyNestedClass {
public String showYourMind = "I love you so much";
}
// default method
default void howToLove() {
System.out.println("Just show your mind");
}
}
public interface IMy2 extends IMy {
@Override
default void howToLove() {
}
// nested type, class 即便是同名,也不会重写啊
class MyNestedClass {
public String showYourMind = "I love you so much";
}
// static method, 这个静态方法,其实并不是继承的父接口静态方法, 因为父类的静态方法只能被父类调用, 子类调不到的
static void loveForever(String[] args) {
System.out.println("args = [" + Arrays.toString(args) + "]"+"I live you my dear ");
}
}
public class ImplIMy implements IMy {
@Override
public void showYourMind() {
}
// instance method, 这个实例方法,其实并不是继承的父接口静态方法, 因为父类的静态方法只能被父类调用, 子类调不到的
void loveForever(String[] args) {
System.out.println("args = [" + Arrays.toString(args) + "]");
}
}
IMy2.loveForever(new String[]{"beautiful girl"});
ImplIMy implIMy = new ImplIMy();
implIMy.loveForever(new String[]{"catalina"});
ImplIMy.loveForever(new String[]{"catalina"});
报错:
static method may be invoked on containing interface class only
8.4.8. Inheritance, Overriding, and Hiding
A class C inherits from its direct superclass all concrete methods m
(both static
and instance) of the superclass for which all of the following are true:
m
is a member of the direct superclass of C.m
ispublic
,protected
, or declared with package access in the same package as C.No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of
m
.
A class C inherits from its direct superclass and direct superinterfaces all abstract
and default (§9.4) methods m
for which all of the following are true:
m
is a member of the direct superclass or a direct superinterface, D, of C.m
ispublic
,protected
, or declared with package access in the same package as C.No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of
m
.No concrete method inherited by C from its direct superclass has a signature that is a subsignature of the signature of
m
.There exists no method
m
' that is a member of the direct superclass or a direct superinterface, D', of C (m
distinct fromm
', D distinct from D'), such thatm
' from D' overrides the declaration of the methodm
.
A class does not inherit static
methods from its superinterfaces.
Note that it is possible for an inherited concrete method to prevent the inheritance of an abstract
or default method. (Later we will assert that the concrete method overrides the abstract
or default method "from C".) Also, it is possible for one supertype method to prevent the inheritance of another supertype method if the former "already" overrides the latter - this is the same as the rule for interfaces (§9.4.1), and prevents conflicts in which multiple default methods are inherited and one implementation is clearly meant to supersede the other.
Note that methods are overridden or hidden on a signature-by-signature basis. If, for example, a class declares two public
methods with the same name (§8.4.9), and a subclass overrides one of them, the subclass still inherits the other method.
- 抽象类的静态方法不可被subclass的同名同参实例方法覆写或隐藏
public abstract class MyAbstract {
// static method
static void loveForever(String[] args) {
System.out.println("args = [" + Arrays.toString(args) + "]");
}
}
public class MyAbstactImpl extends MyAbstract {
// static method
报错:
instance method cannot override static method
void loveForever(String[] args) {
System.out.println("args = [" + Arrays.toString(args) + "]");
}
}
public class MyAbstactImpl extends MyAbstract {
// static method , 覆写了
static void loveForever(String[] args) {
System.out.println("args = [" + Arrays.toString(args) + "]");
}
}
- 抽象类,subclass实例可以继承到父抽象类的静态方法
public abstract class MyAbstract {
// static method
static void loveForever(String[] args) {
System.out.println("args = [" + Arrays.toString(args) + "]");
}
}
public class MyAbstactImpl extends MyAbstract {
}
MyAbstactImpl myAbstact = new MyAbstactImpl();
myAbstact.loveForever(new String[]{"sure"});
MyAbstactImpl.loveForever(new String[]{"sure"});