接口概述:

接口是Java语言中的一种援用类型,是办法的"汇合",所以接口的外部次要就是定义方法,蕴含常量,形象办法(JDK 7及以前),额定减少默认办法和静态方法(JDK 8),额定减少公有办法(jdk9)。

接口的定义,它与定义类形式类似,然而应用 interface 关键字。它也会被编译成.class文件,但肯定要明确它并不是类,而是另外一种援用数据类型。

public class 类名.java–>.class

public interface 接口名.java–>.class

接口的应用,它不能创建对象,然而能够被实现(implements ,相似于被继承)。一个实现接口的类(能够看做是接口的子类),须要实现接口中所有的形象办法,创立该类对象,就能够调用办法了,否则它必须是一个抽象类。

定义格局

public interface 接口名称 {    // 常量    // 形象办法    // 默认办法(jdk8)    // 静态方法(jdk8)    // 公有办法(jdk9)}

案例

public interface IA {    // 常量 默认修饰符 public static final  这三个修饰符能够省略    public static final int NUM1 = 10;    int NUM2 = 20;    // 形象办法 默认修饰符 public abstract 这2个修饰符能够省略    public abstract void method1();    void method2();    // 默认办法 默认修饰符 public default public修饰符能够省略,default不能够省略    public default void method3(){        System.out.println("默认办法 method3");    }    default void method4(){        System.out.println("默认办法 method4");    }    // 静态方法: public static润饰  static修饰符不能够省略 public能够省略    public static void method5(){        System.out.println("静态方法 method5");    }     // 公有静态方法 应用private static润饰  不能够省略     private static void method6(){        System.out.println("公有静态方法 method6");    }    // 公有非静态方法 应用private润饰    private  void method7(){        System.out.println("公有静态方法 method7");    }}public class Test {    public static void main(String[] args) {               System.out.println(IA.NUM1);// 10    }    // 类中的默认办法,应用默认权限修饰符(空)    void method(){    }}

接口中成员的拜访特点

接口中成员拜访特点概述

  接口中成员的拜访特点:                接口中的常量: 次要是供接口间接应用                接口中的形象办法: 供实现类重写的                接口中的默认办法: 供实现类继承的(实现类中能够间接调用,实现类对象也能够间接调用)                接口中的静态方法: 只供接口间接调用,实现类继承不了                接口中的公有办法: 只能在接口中间接调用,实现类继承不了

案例演示

public interface IA {    //  接口中的常量: 次要是供接口间接应用    public static final int NUM = 10;    // 接口中的形象办法: 供实现类重写的    public abstract void method1();    // 接口中的默认办法: 供实现类继承应用(实现类中能够间接调用,实现类对象也能够间接调用)    public default void method2(){        System.out.println("默认办法method2");        method4();        method5();    }    // 接口中的静态方法: 只供接口间接调用,实现类继承不了    public static void method3(){        System.out.println("静态方法method3");        method5();    }    // 接口中的公有办法: 只能在接口中间接调用,实现类继承不了    private void method4(){// 只能在接口的默认办法中调用        // 办法体        method5();    }    private static void method5(){//        // 办法体    }}实现类:public class ImpA implements IA{   /* @Override    public void method2() {    }*/    @Override    public void method1() {        System.out.println("重写接口中的method1形象办法");    }}测试类:public class Test {    public static void main(String[] args) {              System.out.println(IA.NUM);// 10        // 创立实现类对象,拜访NUM常量        ImpA ia = new ImpA();        System.out.println(ia.NUM);// 10        // 调用method2办法        ia.method2();        // 通过接口名调用接口中的静态方法        IA.method3();        //ia.method3();// 编译报错,    }}

多实现时的几种抵触状况

私有动态常量的抵触: 多个父接口中,雷同的常量不能被继承 - 私有形象办法的抵触: 实现类必须重写一次
私有默认办法的抵触: 实现类必须重写
私有静态方法的抵触: 无影响,因为静态方法实现类不能继承
公有办法的抵触: 无影响,因为公有办法只能在本接口中间接拜访,实现类不能继承
实现类重写接口中的默认办法,不须要加default

私有动态常量的抵触

实现类不继承抵触的变量

interface IA{    public static final int a = 10;    public static final int b= 20;}interface IB{    public static final int a = 30;}class Zi implements IA,IB{    //只继承了b,没有继承a,因为a抵触了}public class Demo {    public static void main(String[] args) {        Zi z = new Zi();     //   System.out.println(z.a);//编译谬误        System.out.println(z.b);    }}

私有形象办法的抵触

实现类只须要重写一个

interface IA{    public void show();}interface IB{    public void show();}class Zi implements IA,IB{    @Override    public void show() {//子类只须要重写一个show()即可        System.out.println("子类的show()...");    }}public class Demo {    public static void main(String[] args) {        Zi z = new Zi();        z.show();    }}

私有默认办法的抵触

实现类必须重写一次最终版本

interface IA{    public default void show(){        System.out.println("IA");    }}interface IB{    public default void show(){        System.out.println("IB");    }}class Zi implements IA,IB{    @Override    public void show() {//必须重写一次的show()        System.out.println("Zi的show()....");    }}public class Demo {    public static void main(String[] args) {        Zi z = new Zi();        z.show();    }}

私有静态方法的抵触

静态方法是间接属于接口的,不能被继承,所以不存在抵触

interface IA{    public static  void show(){        System.out.println("IA");    }}interface IB{    public static void show(){        System.out.println("IB");    }}class Zi implements IA,IB{}public class Demo {    public static void main(String[] args) {        Zi z = new Zi();        z.show();//编译谬误,show()不能被继承。    }}

公有办法的抵触

公有办法只能在本接口中间接应用,不存在抵触

接口和接口的关系

接口能够“继承”自另一个“接口”,而且能够“多继承”

interface IA{}interface IB{}interface IC extends IA,IB{//是“继承”,而且能够“多继承”}

接口继承接口的抵触状况

私有动态常量的抵触: 不能被继承,应用不了
私有形象办法的抵触: 只继承一个
私有默认办法的抵触: 必须重写一次
私有静态方法和公有办法的抵触 : 无影响,因为不能被子接口继承

私有动态常量的抵触

interface IA{    public static final int a = 10;    public static final int b = 30;}interface IB{    public static final int a = 20;}interface IC extends IA,IB{//没有继承a}//测试:main(){    System.out.println(IC.a);//谬误的}

私有形象办法抵触

interface IA{    public void show();}interface IB{    public void show();}interface IC extends IA,IB{//IC只继承了一个show()}class Zi implements IC{    //重写一次show()    public void show(){    }}

私有默认办法的抵触

interface IA{    public default void d1(){    }}interface IB{    public default void d1(){    }}interface IC extends IA,IB{//必须重写一次d1()    public default  void d1(){    }}

私有静态方法和公有办法

不抵触,因为静态方法是间接属于接口的,只能应用接口间接拜访,而公有办法只能在接口中拜访,也没有抵触
实现类继承父类又实现接口时的抵触

定义格局

public class 实现类名 extends 父类名 implements 接口名1,接口名2,...{           }

实现类继承父类又实现接口时的抵触:

私有动态常量的抵触–>没有继承
私有形象办法的抵触—>重写
私有默认办法的抵触—>优先父类
私有静态方法---->优先父类
公有办法的抵触—> 没有抵触

父类和接口的私有动态常量的抵触

class Fu{    public static final int a = 10;}interface IA{    public static final int a = 20;}class Zi extends Fu implements IA{//没有继承a变量}public class Demo {    public static void main(String[] args) {        System.out.println(Zi.a);//编译谬误    }}

父类和接口的形象办法抵触

abstract class Fu{    public abstract void show();}interface IA{    public void show();}class Zi extends Fu implements IA{// 必须重写    }//测试:main(){    Zi z = new Zi();    z.show();//a}

父类和接口的私有默认办法的抵触

class Fu{    public void show(){        System.out.println("a");    }}interface IA{    public default void show(){        System.out.println("b");    }}class Zi extends Fu implements IA{}//测试:main(){    Zi z = new Zi();    z.show();//a}

父类和接口的私有静态方法

class Fu{    public static void show(){        System.out.println("fu...");    }}interface IA{    public static void show(){        System.out.println("IA...");    }}class Zi extends Fu implements IA{//只继承了"父类"的静态方法,没有继承接口的静态方法}public class Demo {    public static void main(String[] args) {        Zi.show();//fu…    }}

父类和接口的公有办法

不存在抵触

最初

大家看完有什么不懂的能够在下方留言探讨,也能够关注我私信问我,我看到后都会答复的。也欢送大家关注我的公众号:前程有光,金三银四跳槽面试季,整顿了1000多道将近500多页pdf文档的Java面试题材料,文章都会在外面更新,整顿的材料也会放在外面。谢谢你的观看,感觉文章对你有帮忙的话记得关注我点个赞反对一下!