为什么要使用接口,多重继承与接口,接口的好处? [英] Why to use Interfaces, Multiple Inheritance vs Interfaces, Benefits of Interfaces?

查看:52
本文介绍了为什么要使用接口,多重继承与接口,接口的好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这件事仍有一些困惑.到目前为止我发现的是

(这里已经问过类似的问题,但我还有其他一些观点.)

<块引用>

  1. 接口仅包含抽象方法和最终字段.

  2. Java 中没有多重继承.

  3. 接口可用于在 Java 中实现多重继承.

  4. 继承的一个优点是我们可以在派生类中使用基类的代码而无需再次编写.可能这是继承存在的最重要的事情.

现在..

<块引用>

第一季度.由于接口只有抽象方法(没有代码),所以我们怎么能说如果我们正在实现任何接口,那么它就是继承?我们没有使用它的代码.

第 2 季度.如果实现一个接口不是继承,那么接口是如何实现多重继承的?

第三季度.无论如何,使用接口有什么好处?他们没有任何代码.我们需要在我们实现它的所有类中一次又一次地编写代码.

那为什么要做接口呢?

注意:我发现了一种界面很有帮助的情况.一个例子就像在 Runnable 接口中,我们有 public void run() 方法,在该方法中我们定义了线程的功能,并且内置了代码,该方法将作为单独的线程运行.所以我们只需要编写代码在线程中做什么,Rest 是预定义的.不过这个东西也可以用抽象类什么的来实现.

那么使用接口的具体好处是什么?我们使用接口实现的真的是多重继承吗?

解决方案

接口是最终静态字段和抽象方法的集合(新的 Java 8 添加了对在接口中包含静态方法的支持).

接口是在我们知道某些任务必须完成的情况下制作的,但应该如何完成可能会有所不同.换句话说,我们可以说我们实现了接口,以便我们的类开始以特定方式运行.

举个例子,大家都知道什么是动物.就像狮子是动物,猴子是动物,大象是动物,牛是动物等等.现在我们知道所有的动物都会吃东西和睡觉.但是每只动物吃东西或睡觉的方式可能不同.就像狮子通过狩猎其他动物来吃东西,而牛吃草.但是两个都吃.所以我们可以有一些这样的伪代码,

interface Animal {公共无效吃();公共无效睡眠();}类 Lion 实现 Animal {公共无效吃(){//狮子的吃法}公共无效睡眠(){//狮子睡觉的方式}}类猴子实现动物{公共无效吃(){//猴子的吃法}公共无效睡眠(){//猴子的睡眠方式}}

根据上面提到的伪代码,凡是能吃能睡的都叫动物,也可以说是所有动物都要吃饭睡觉,但是吃饭睡觉的方式因动物而异.

在接口的情况下,我们只继承行为,而不是类继承的实际代码.

<块引用>

第一季度.由于接口只有抽象方法(没有代码),所以我们怎么能说如果我们正在实现任何接口,那么它就是继承?我们没有使用它的代码.

实现接口是另一种继承.它不同于类的继承,因为继承子类从基类中获取真正的代码以进行重用.

<块引用>

第 2 季度.如果实现一个接口不是继承,那么接口是如何实现多重继承的?

据说是因为一个类可以实现多个接口.但是我们需要明白,这种继承与类的继承是不同的.

<块引用>

第三季度.无论如何,使用接口有什么好处?他们没有任何代码.我们需要在我们实现它的所有类中一次又一次地编写代码.

实现接口会强制类必须覆盖其所有抽象方法.

在我的书中阅读更多内容这里这里

I still have some confusion about this thing. What I have found till now is

(Similar questions have already been asked here but I was having some other points.)

  1. Interface is collection of ONLY abstract methods and final fields.

  2. There is no multiple inheritance in Java.

  3. Interfaces can be used to achieve multiple inheritance in Java.

  4. One Strong point of Inheritance is that We can use the code of base class in derived class without writing it again. May be this is the most important thing for inheritance to be there.

Now..

Q1. As interfaces are having only abstract methods (no code) so how can we say that if we are implementing any interface then it is inheritance ? We are not using its code.

Q2. If implementing an interface is not inheritance then How interfaces are used to achieve multiple inheritance ?

Q3. Anyhow what is the benefit of using Interfaces ? They are not having any code. We need to write code again and again in all classes we implement it.

Then why to make interfaces ?

NOTE : I have found one case in which interfaces are helpful. One example of it is like in Runnable interface we have public void run() method in which we define functionality of thread and there is built in coding that this method will be run as a separate thread. So we just need to code what to do in thread, Rest is pre-defined. But this thing also can be achieved using abstract classes and all.

Then what are the exact benefits of using interfaces? Is it really Multiple-Inheritance that we achieve using Interfaces?

解决方案

Interfaces are collection of final static fields and abstract methods (Newly Java 8 added support of having static methods in an interface).

Interfaces are made in situations when we know that some task must be done, but how it should be done can vary. In other words we can say we implement interfaces so that our class starts behaving in a particular way.

Let me explain with an example, we all know what animals are. Like Lion is an animal, monkey is an animal, elephant is an animal, cow is an animal and so on. Now we know all animals do eat something and sleep. But the way each animal can eat something or sleep may differ. Like Lion eats by hunting other animals where as cow eats grass. But both eat. So we can have some pseudo code like this,

interface Animal {
    public void eat();
    public void sleep();   
}

class Lion implements Animal {
    public void eat() {
        // Lion's way to eat
    }

    public void sleep(){
         // Lion's way to sleep
    }
}

class Monkey implements Animal {
    public void eat() {
        // Monkey's way to eat
    }

    public void sleep() {
        // Monkey's way to sleep
    }
}

As per the pseudo code mentioned above, anything that is capable of eating or sleeping will be called an animal or we can say it is must for all animals to eat and sleep but the way to eat and sleep depends on the animal.

In case of interfaces we inherit only the behaviour, not the actual code as in case of classes' inheritance.

Q1. As interfaces are having only abstract methods (no code) so how can we say that if we are implementing any interface then it is inheritance ? We are not using its code.

Implementing interfaces is other kind of inheritance. It is not similar to the inheritance of classes as in that inheritance child class gets the real code to reuse from the base class.

Q2. If implementing an interface is not inheritance then How interfaces are used to achieve multiple inheritance ?

It is said because one class can implement more than one interfaces. But we need to understand that this inheritance is different than classes' inheritance.

Q3. Anyhow what is the benefit of using Interfaces ? They are not having any code. We need to write code again and again in all classes we implement it.

Implementing an interface puts compulsion on the class that it must override its all abstract methods.

Read more in my book here and here

这篇关于为什么要使用接口,多重继承与接口,接口的好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆