接口概述:
接口是 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 面试题资料, 文章都会在里面更新, 整理的资料也会放在里面. 谢谢你的观看, 觉得文章对你有帮助的话记得关注我点个赞支持一下!
来源: http://www.bubuko.com/infodetail-3643821.html