java 抽象类和多重继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38167825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:11:58  来源:igfitidea点击:

Abstract classes and Multiple Inheritance

javainterfaceabstract-classmultiple-inheritance

提问by Zephyr

We can achieve the same functionality as interfaces by using abstract classes, So why java doesn't allow the following code?

我们可以通过使用抽象类来实现与接口相同的功能,那么为什么java不允许下面的代码呢?

abstract class Animals
{
    public abstract void run();
}

abstract class Animals1
{
    public abstract void run1();
}

class Dog extends Animals,Animals1
{
  public void run() {System.out.println("Run method");}
  public void run1() {System.out.println("Run1 method");}
}

I know that multiple inheritance can be achieved by using only interfaces but the above code does the same thing as the interfaces would have done it.

我知道可以通过仅使用接口来实现多重继承,但上面的代码与接口所做的事情相同。

回答by Peter Lawrey

This is not allowed because you can do more than this with abstract classes. It wouldn't make sense to allow multiple inheritance, provided you only used an abstract class when you could have used an interface.

这是不允许的,因为您可以使用抽象类做更多的事情。允许多重继承是没有意义的,前提是您只在可以使用接口时才使用抽象类。

It is simpler to only use abstract classes for things you can't do with an interface, in which case you wouldn't be able to use two abstract parent classes.

仅将抽象类用于无法使用接口执行的操作会更简单,在这种情况下,您将无法使用两个抽象父类。

Note: with Java 8 there is less you can't do with an interface, you can have public instance and static methods with implementations.

注意:在 Java 8 中,你不能用接口做的事情少了,你可以有公共实例和静态方法和实现。

In Java 9 you will be able to have privatemethods in interfaces ;)

在 Java 9 中,您将能够private在接口中使用方法;)

回答by meJustAndrew

This is because abstract classes are still classes, and inheritance is different than implementing an interface. Please see the differences between abstract class and interface. Also please see differences between inplementing an interface and extending a classin Java.

这是因为抽象类仍然是类,继承与实现接口不同。请查看抽象类和接口之间的区别。另请参阅在 Java 中实现接口和扩展类之间的区别

This both questions covers all the information you need to know to understand why you can't have multiple inheritance of abstract classes.

这两个问题都涵盖了您需要了解的所有信息,以了解为什么您不能拥有抽象类的多重继承。

Also let me give you an example why this should not be allowed: Suppose you have the both Animalsand Animals1implementing the same interface IAnimals:

还让我举一个例子,为什么不应该允许这样做:假设您拥有两者 AnimalsAnimals1实现相同的接口IAnimals

interface IAnimals
{
    public string eat();
}

abstract class Animals implements IAnimals
{
    public abstract void run();
    public string eat(){ return "Animals eating"; } 
}

abstract class Animals1 implements IAnimals
{
    public abstract void run1();
    public string eat(){ return "Animals1 eating"; } 
}

If you now define your Dogclass as you did:

如果您现在Dog像以前一样定义类:

class Dog extends Animals,Animals1
{
  public void run() {System.out.println("Run method");}
  public void run1() {System.out.println("Run1 method");}
}

It will have the method eat()too, which is not abstract so it can use it directly. What would be the return of this method for a dog? Which string will be returned, the one with Animals, or the one with Animals1?

它也会有方法eat(),它不是抽象的,所以它可以直接使用它。这种方法对狗的回报是什么?将返回哪个字符串,带有 Animals 的字符串,还是带有 Animals1 的字符串?

This is called the diamond problem, and it is a reason why in some programming languages it is not allowed multiple inheritance.

这被称为钻石问题,这也是为什么在某些编程语言中不允许多重继承的原因。

回答by Rini

Java does not support multiple Inheritance. -" One reason why the Java programming language does not permit you to extend more than one class is to avoid the issues of multiple inheritance of state, which is the ability to inherit fields from multiple classes. " Source https://docs.oracle.com/javase/tutorial/java/IandI/multipleinheritance.htmlYou may find this link useful.

Java 不支持多重继承。-" Java 编程语言不允许扩展多个类的原因之一是为了避免状态的多重继承问题,即从多个类继承字段的能力。" 来源https://docs.oracle .com/javase/tutorial/java/IandI/multipleinheritance.html您可能会发现此链接很有用。

回答by Abhijit

I agree with you(if i understood about what you are talking :) )this is no need of such specific naming conventions.

我同意你的看法(如果我明白你在说什么 :) )这不需要这种特定的命名约定。

interface pet
{

    public abstract void pet();
}

interface pet1
{
    public abstract void pet1();
}

class TestTt implements pet,pet1
{
    public void pet() 
    {
        System.out.println("this is method of pet interface");

    }

    public void pet1() {
        System.out.println("this is method of pet1 interface");

    }   
    public static void main(String a[])
    {
        pet obj=new TestTt();

        pet1 obj1=new TestTt(); 

        obj.pet();

        obj1.pet1();
    }       
}

Now, Here if abstract class allows me to create object .then, i can create 2 different references for 2 abstract classes as in interface i can do.

现在,如果抽象类允许我创建对象。那么,我可以像在接口中一样为 2 个抽象类创建 2 个不同的引用。

If so, do i need Interfaces...?

如果是这样,我需要接口吗...?

回答by Rohit Mehta

In ABSTRACT class,we can't extends multiple abstract classes at a time. but In INTERFACE, we can implements multiple interfaces at time.

在 ABSTRACT 类中,我们不能一次扩展多个抽象类。但是在 INTERFACE 中,我们可以同时实现多个接口。

Therefore , interfaces are used to achieve multiple inheritance in java.

因此,java中使用接口来实现多重继承。