Interface和abstract class

    这几天做实验的时候总是会碰到interface和abstract class之间的选择的问题,细想一下发现这两个概念之间确确实实有许多的相似之处,现在将二者的相似之处和不同点在网上找了一番并且总结如下:


一、语法层面的区别

Java语言对interface和abstract class的语法做了很明确的定义,先累述如下:

    1.1 interface

     Java中interface的定义语法如下

//Code from Wikipedia at https://en.wikipedia.org/wiki/Abstract_type#Example_(Java)
//By default, all methods in all interfaces are abstract, unless the default keyword is used.
interface DemoInterface {
    [abstract] int getLength(); //Abstract can be used here, though is completely useless
    
    //The default keyword can be used in this context to specify a concrete method in an interface
    default int product(int x, int y) {
        return x * y;
    }
}

    interface中还可以有方法的默认行为(可以从这篇博客中找到详细的解答)

    1.2 abstract class

    Java中的abstract class的定义语法如下

//Code from Wikipedia at https://en.wikipedia.org/wiki/Abstract_type#Example_(Java)
//By default, all methods in all classes are concrete, unless the abstract keyword is used.
abstract class Demo {
    // An abstract class may include abstract methods, which have no implementation.
    abstract public int sum(int x, int y);

    // An abstract class may also include concrete methods.
    public int product(int x, int y) { return x*y; }
}

二、编程上的区别

    对于abstract class,它体现的更多的是一种继承类的关系,一个类只可以继承一个abstract class但是一个类却可以实现多个interface。

    其次,按照这个问答中的说法,一个interface占用的CPU较少,因为interface并不是一个class而仅仅是一群方法体的集合。而与之对立的是,一个abstract class可能会消耗掉更多的资源,因为在继承的时候会有一个搜索(look up)的操作。

    再其次,在interface中只能有静态的不能被修改的数据成员,所有不是默认方法的方法都是抽象的,而对于abstract class他可以由自己的数据成员,也可以有自己的非abstract方法,事实上如果不用abstract关键字指定的话,abstract class的方法默认都是非抽象的


三、思想上的区别

    依旧是按照这篇问答上的回答,一个interface更像是一个人写了一个空的躯壳(empty shell),也像是一个interface和实现interface的class之间达成的协议,写interface的人说:“我写了一个interface,它长成这个样子!”,实现interface的人就回答说:“OK,那我就拿着你给我的样子来写一个实现好了。”,这是那篇问答中的一个例子:

//Code from stack overflow at https://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class
// I say all motor vehicles should look like this:
interface MotorVehicle
{
    void run();

    int getFuel();
}

// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}
// I say all motor vehicles should look like this:
interface MotorVehicle
{
    void run();

    int getFuel();
}

// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{

    int fuel;

    void run()
    {
        print("Wrroooooooom");
    }


    int getFuel()
    {
        return this.fuel;
    }
}

        然而对于abstract class来说,更像是一个人已经写好了一些代码,我们要做的更像是填空,比如,写抽象类的人说:“嗯,我觉得所有的车都是要有油的,那么我定义一个油,一个加油的方法,然而对于跑的方法,可能每一个车都有每一个车的方法,我就不写了,你们来填空吧!”

//Code from stack overflow at https://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class
// I say all motor vehicles should look like this:
abstract class MotorVehicle
{

    int fuel;

    // They ALL have fuel, so lets implement this for everybody.
    int getFuel()
    {
         return this.fuel;
    }

    // That can be very different, force them to provide their
    // own implementation.
    abstract void run();
}

// My teammate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
    void run()
    {
        print("Wrroooooooom");
    }
}

同时这篇文章的第三部分也给出了一个很棒的例子来阐释这两个概念的不同,文章写的很棒,在这里觉得没有必要再重复一遍了。


四、一点声明

    这篇文章说是打着原创的标签,几乎所有的内容包括组织结构在内都是参考stack overflow上的回答以及《深入理解abstract和interface》这篇博文,如果您认为这个侵权行为,我愿意向您道歉并且马上删除这篇博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值