java8 接口

  • 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 inherit static 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 is public, 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 is public, 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 from m', D distinct from D'), such that m' from D' overrides the declaration of the method m.

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.

4165335-182dc7c0e7722725.png
image.png
  • 抽象类的静态方法不可被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"});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值