接口是Java语言中的一种引用类型,是方法的"集合",因此接口的内部主要就是定义方法,包含常量,抽象方法(JDK 7及之前),额外增长默认方法和静态方法(JDK 8),额外增长私有方法(jdk9)。java
接口的定义,它与定义类方式类似,可是使用 interface 关键字。它也会被编译成.class文件,但必定要明确它并非类,而是另一种引用数据类型。面试
public class 类名.java–>.classide
public interface 接口名.java–>.class测试
接口的使用,它不能建立对象,可是能够被实现(implements ,相似于被继承)。一个实现接口的类(能够看作是接口的子类),须要实现接口中全部的抽象方法,建立该类对象,就能够调用方法了,不然它必须是一个抽象类。code
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(); } }
静态方法是直接属于接口的,不能被继承,因此不存在冲突it
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面试题资料,文章都会在里面更新,整理的资料也会放在里面。谢谢你的观看,以为文章对你有帮助的话记得关注我点个赞支持一下!