java8的interface和abstract class

一:java8接口interface的变化

1. 在java8中接口可以有default方法和static方法。java8之前的接口中的方法全部默认为 public abstract method_name(),变量全部默认为public static final

default method in interface 默认方法

简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法。只需在方法名前面加个default关键字即可。 

interface Drawable{  
void draw();  
default public void msg(){System.out.println("default method");}  
}  
class Rectangle implements Drawable{  
public void draw(){System.out.println("drawing rectangle");}  
}  
class TestInterfaceDefault{  
public static void main(String args[]){  
Drawable d=new Rectangle();  
d.draw();  
d.msg();  
}}  

default方法是提交提供的一个默认实现,也可以像其他方法一样被继承。接口可以有多继承,当多个父接口中有相同签名的默认方法时,子实现类中需要以

<Interface接口名>.super.<method方法名>()的形式指定要调用哪个父接口中的方法

由于同一个方法可以从不同接口引入,自然而然的会有冲突的现象,默认方法判断冲突的规则如下:

1.一个声明在类里面的方法优先于任何默认方法(classes always win)

2.否则,则会优先选取最具体的实现,比如下面的例子 B重写了A的toOverride方法。

interface A
{
    void foo();
    default void toOverride() {
        System.out.println("A");
    }
}
interface B extends A
{
    default void toOverride() {
        A.super.toOverride();
        System.out.println("B");
    }
}
interface C extends B
{
    default void toOverride() {
        A.super.toOverride();// does not work
        B.super.toOverride();
        System.out.println("B");
    }
}
class D implements B
{
    public void toOverride() {

    }    
    public void foo() {
        D.this.toOverride();
        B.super.toOverride();
        A.super.toOverride(); // does not work!
    }
}


static method in interface  通过接口名就可以直接调用其静态方法

interface Drawable{  
void draw();  
static int cube(int x){return x*x*x;}  
}  
class Rectangle implements Drawable{  
public void draw(){System.out.println("drawing rectangle");}  
}  
  
class TestInterfaceStatic{  
public static void main(String args[]){  
Drawable d=new Rectangle();  
d.draw();  
System.out.println(Drawable.cube(3));  
}}  


二:接口interface和抽象类abstract class的区别

Abstract class Interface
1) Abstract class can have abstract and non-abstractmethods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
6) Example:
public abstract class Shape{
public abstract void draw();
}
Example:
public interface Drawable{
void draw();

7)接口中没有 this 指针,没有构造函数. 抽象类中可以有  

注意:

  • default 关键字只能在接口中使用(以及用在 switch 语句的 default 分支),不能用在抽象类中。

  • 接口默认方法不能覆写 Object 类的 equalshashCode 和 toString 方法。

  • 接口中的静态方法必须是 public 的,public 修饰符可以省略,static 修饰符不能省略。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值