简体   繁体   English

Java 接口如何模拟多重继承?

[英]How do Java Interfaces simulate multiple inheritance?

I am reading "The Java Tutorial" (for the 2nd time).我正在阅读“Java 教程”(第二次)。 I just got through the section on Interfaces (again), but still do not understand how Java Interfaces simulate multiple inheritance.我刚刚(再次)浏览了接口部分,但仍然不明白 Java 接口如何模拟多重继承。 Is there a clearer explanation than what is in the book?有没有比书上更清楚的解释?

Suppose you have 2 kinds of things in your domain : Trucks and Kitchens假设您的域中有两种东西:卡车和厨房

Trucks have a driveTo() method and Kitchens a cook() method.卡车有一个 driveTo() 方法,而厨房有一个 Cook() 方法。

Now suppose Pauli decides to sell pizzas from the back of a delivery truck.现在假设泡利决定从一辆送货卡车的后面卖比萨饼。 He wants a thing where he can driveTo() and cook() with.他想要一个他可以用 driveTo() 和 cook() 的东西。

In C++ he would use multiple inheritance to do this.在 C++ 中,他会使用多重继承来做到这一点。

In Java that was considered to be too dangerous so you can inherit from a main class, but you can "inherit" behaviors from interfaces, which are for all intents and purposes abstract classes with no fields or method implementations.在 Java 中,这被认为太危险了,因此您可以从主类继承,但您可以从接口“继承”行为,这些行为出于所有意图和目的都是没有字段或方法实现的抽象类。

So in Java we tend to implement multiple inheritance using delegations :所以在 Java 中,我们倾向于使用委托来实现多重继承:

Pauli subclasses a truck and adds a kitchen to the truck in a member variable called kitchen. Pauli 将卡车子类化,并在名为 kitchen 的成员变量中向卡车添加厨房。 He implements the Kitchen interface by calling kitchen.cook().他通过调用 kitchen.cook() 来实现 Kitchen 接口。

class PizzaTruck extends Truck implements Kitchen {
   Kitchen kitchen;

   public void cook(Food foodItem) {
      kitchen.cook(foodItem);
   }
}

He is a happy man because he can now do things like ;他是一个快乐的人,因为他现在可以做这样的事情;

pizzaTruck.driveTo(beach);
pizzaTruck.cook(pizzaWithExtraAnchovies);

Ok, this silly story was to make the point that it is no simulation of multiple inheritance, it is real multiple inheritance with the proviso that you can only inherit the contract, only inherit from empty abstract base classes which are called interfaces.好吧,这个愚蠢的故事是要说明它不是模拟多重继承,它是真正的多重继承,条件是您只能继承契约,只能从称为接口的空抽象基类继承。

(update: with the coming of default methods interfaces now can also provide some behavior to be inherited) (更新:随着默认方法接口的出现,现在也可以提供一些可以被继承的行为)

You're probably confused because you view multiple inheritance locally, in terms of one class inheriting implementation details from multiple parents.您可能会感到困惑,因为您在本地查看多重继承,就一个类从多个父级继承实现细节而言。 This is not possible in Java (and often leads to abuse in languages where it's possible).这在 Java 中是不可能的(并且在可能的情况下经常导致语言滥用)。

Interfaces allow multiple inheritance of types , eg a class Waterfowl extends Bird implements Swimmer can be used by other classes as if it were a Bird and as if it were a Swimmer .接口允许类型的多重继承,例如一个class Waterfowl extends Bird implements Swimmer可以被其他类使用,就好像它是一只Bird 也好像它是一个Swimmer This is the the deeper meaning of multiple inheritance: allowing one object to act like it belongs to several unrelated different classes at once.这就是多重​​继承的深层含义:允许一个对象表现得好像它同时属于几个不相关的不同类。

Here is a way to achieve multiple inheritance through interfaces in java.下面介绍一种在java中通过接口实现多重继承的方法。

What to achieve?要达到什么目的?
class A extends B, C // this is not possible in java directly but can be achieved indirectly. class A extends B, C // 这在 java 中是不可能直接实现的,但可以间接实现。

class B{
   public void getValueB(){}
}

class C{
   public void getValueC(){}
}


interface cInterface{
   public getValueC();
}

class cChild extends C implemets cInterface{
    public getValueC(){

      // implementation goes here, call the super class's getValueC();

    }
}


// Below code is **like** class A extends B, C 
class A extends B implements cInterface{
   cInterface child =  new cChild();
   child.getValueC();
}

given the two interfaces below...鉴于下面的两个接口......

interface I1 {
  abstract void test(int i);
}
interface I2 {
  abstract void test(String s);
}

We can implement both of these using the code below...我们可以使用下面的代码实现这两个......

public class MultInterfaces implements I1, I2 {
  public void test(int i) {
    System.out.println("In MultInterfaces.I1.test");
  }
  public void test(String s) {
    System.out.println("In MultInterfaces.I2.test");
  }
  public static void main(String[] a) {
    MultInterfaces t = new MultInterfaces();
    t.test(42);
    t.test("Hello");
  }
}

We CANNOT extend two objects, but we can implement two interfaces.我们不能扩展两个对象,但我们可以实现两个接口。

Interfaces don't simulate multiple inheritance.接口不模拟多重继承。 Java creators considered multiple inheritance wrong, so there is no such thing in Java. Java 创建者认为多重继承是错误的,所以 Java 中没有这种东西。

If you want to combine the functionality of two classes into one - use object composition.如果您想将两个类的功能合二为一 - 使用对象组合。 Ie IE

public class Main {
    private Component1 component1 = new Component1();    
    private Component2 component2 = new Component2();
}

And if you want to expose certain methods, define them and let them delegate the call to the corresponding controller.如果您想公开某些方法,请定义它们并让它们将调用委托给相应的控制器。

Here interfaces may come handy - if Component1 implements interface Interface1 and Component2 implements Interface2 , you can define这里接口可能会派上用场 - 如果Component1实现接口Interface1并且Component2实现Interface2 ,您可以定义

class Main implements Interface1, Interface2

So that you can use objects interchangeably where the context allows it.这样您就可以在上下文允许的情况下互换使用对象。

You know what, coming from the perspective of a JavaScript dev trying to understand what the heck is going on with this stuff, I'd like to point out a couple things and somebody please tell me what I'm missing here if I'm way off the mark.你知道吗,从一个 JavaScript 开发者的角度试图理解这些东西到底发生了什么,我想指出一些事情,如果我是,请有人告诉我我在这里遗漏了什么偏离目标。

Interfaces are really simple.接口真的很简单。 Stupidly, insanely simple.愚蠢的,疯狂的简单。 They're as stupidly, insanely simple as people initially think, which is why there are so many duplicate questions on this exact subject because the one reason to use them has been made unclear by people trying to make more of them than they are and there is widespread misuse in every Java server-side code-base I've ever been exposed to.它们和人们最初想象的一样愚蠢,疯狂地简单,这就是为什么在这个确切的主题上有这么多重复的问题,因为使用它们的一个原因已经被人们试图制造比它们更多的人弄得不清楚。在我接触过的每个 Java 服务器端代码库中都被广泛滥用。

So, why would you want to use them?那么,为什么要使用它们呢? Most of the time you wouldn't.大多数时候你不会。 You certainly wouldn't want to use them ALL the time as many seem to think.您当然不想像许多人认为的那样一直使用它们。 But before I get to when you would, let's talk about what they're NOT.但在我说你什么时候会这样做之前,让我们先谈谈它们不是什么。

Interfaces are NOT:接口不是:

  • in any way a workaround for any sort of inheritance mechanism that Java lacks.以任何方式解决 Java 缺乏的任何类型的继承机制。 They have nothing to do with inheritance, they never did, and in no way simulate anything inheritance-like.它们与继承无关,它们从来没有做过,也绝不会模拟任何类似继承的东西。
  • necessarily something that helps you with stuff you wrote, so much as it helps the other guy write something meant to be interfaced by your stuff.一定是对你写的东西有帮助的东西,就像它可以帮助另一个人写一些与你的东西交互的东西一样。

They really are as simple as you think they are on first glance.它们确实和您乍一看时认为的一样简单。 People misuse stupidly all the time so it's hard to understand what the point is.人们总是愚蠢地滥用,所以很难理解重点是什么。 It's just validation/testing.这只是验证/测试。 Once you've written something conforms to an interface and works, removing that "implements" code won't break anything.一旦你编写了一些符合接口并且可以工作的东西,删除“实现”代码不会破坏任何东西。

But if you're using interfaces correctly, you wouldn't want to remove it because having it there gives the next developer a tool for writing an access layer for another set of databases or web services that they want the rest of your app to continue using because they know their class will fail until they get the 100% complete-as-expected-interface in place.但是如果你正确使用接口,你不会想要删除它,因为有了它,下一个开发人员就有了一个工具来为另一组数据库或 Web 服务编写访问层,他们希望你的应用程序的其余部分继续使用是因为他们知道他们的课程会失败,直到他们获得 100% 完全符合预期的界面。 All interfaces do is validate your class and establish that you have in fact implemented an interface as you promised you would.所有接口所做的都是验证您的类并确定您实际上已经按照您的承诺实现了一个接口。 Nothing more.而已。

They're also portable.它们也是便携式的。 By exposing your interface definitions you can give people wanting to use your unexposed code a set of methods to conform to in order for their objects to use it correctly.通过公开您的接口定义,您可以为想要使用您未公开代码的人提供一组方法,以便他们的对象正确使用它。 They don't have to implement the interfaces.他们不必实现接口。 They could just jot them down on a piece of notepad paper and double-check that.他们可以把它们记在一张记事本上,然后仔细检查。 But with the interface you have more of a guarantee nothing is going to try to work until it has a proper version of the interface in question.但是有了这个接口,你就更有保证了,除非它有一个合适的接口版本,否则什么都不会尝试工作。

So, any interface not likely to ever be implemented more than once?那么,任何接口都不太可能被多次实现? Completely useless.完全没用。 Multiple-inheritance?多重继承? Stop reaching for that rainbow.停止伸手去拿那道彩虹。 Java avoids them for a reason in the first place and composited/aggregate objects are more flexible in a lot of ways anyway. Java首先出于某种原因避免使用它们,无论如何,复合/聚合对象在很多方面都更加灵活。 That's not to say interfaces can't help you model in ways that multiple-inheritance allows but it's really not inheritance in any way shape or form and shouldn't be seen as such.这并不是说接口不能帮助您以多重继承允许的方式建模,但它实际上不是任何形状或形式的继承,不应该被视为这样。 It's just guaranteeing that your code won't work until you've implemented all of the methods you established that you would.它只是保证你的代码在你实现了你建立的所有方法之前不会工作。

It's pretty simple.这很简单。 You can implement more than one interface in a type.您可以在一种类型中实现多个接口。 So for example, you could have an implementation of List that is also an instance of Deque (and Java does... LinkedList ).例如,您可以有一个List的实现,它也是Deque一个实例(而 Java 确实...... LinkedList )。

You just can't inherit implementations from multiple parents (ie extend multiple classes).您不能从多个父级继承实现(即扩展多个类)。 Declarations (method signatures) are no problem.声明(方法签名)没有问题。

It's not a simulation of multiple inheritance.这不是多重继承的模拟。 In java you can't inherit from two classes, but if you implements two interfaces "it seems like you inherited from two different classes" because you can use your class as any of your two intefaces.在 Java 中,您不能从两个类继承,但是如果您实现了两个接口,“似乎您是从两个不同的类继承的”,因为您可以将您的类用作两个接口中的任何一个。

For example例如

interface MyFirstInteface{
    void method1();
}
interface MySecondInteface{
    void method2();
}
class MyClass implements MyFirstInteface, MySecondInteface{
    public void method1(){
        //Method 1
    }
    public void method2(){
        //Method 2
    }

    public static void main(String... args){
        MyFirstInterface mfi = new MyClass();
        MySecondInterface msi = new MyClass();
    }
}

This will work and you can use mfi and msi, it seems like a multi inheritance, but it's not because you don't inherit anything, you just rewrite public methods provided by the interfaces.这会起作用,你可以使用mfi和msi,它看起来像一个多继承,但这不是因为你没有继承任何东西,你只是重写了接口提供的公共方法。

You need to be precise:你需要准确:

Java allows multiple inheritance of interface, but only single inheritance of implementation. Java 允许接口的多重继承,但只允许实现的单一继承。

You do multiple inheritance of interface in Java like this:您可以像这样在 Java 中对接口进行多重继承:

public interface Foo
{
    String getX(); 
}

public interface Bar
{
    String getY();
}

public class MultipleInterfaces implements Foo, Bar
{
    private Foo foo;
    private Bar bar;

    public MultipleInterfaces(Foo foo, Bar bar)
    {
        this.foo = foo;
        this.bar = bar;
    }

    public String getX() { return this.foo.getX(); }
    public String getY() { return this.bar.getY(); }
}

Just by the way, the reason why Java does not implement full multiple inheritance is because it creates ambiguities.顺便说一句,Java 没有实现完全多重继承的原因是因为它会产生歧义。 Suppose you could say "A extends B, C", and then both B and C have a function "void f(int)".假设您可以说“A extends B, C”,然后B 和C 都有一个函数“void f(int)”。 Which implementation does A inherit? A 继承了哪个实现? With Java's approach, you can implement any number of interfaces, but interfaces only declare a signature.使用 Java 的方法,您可以实现任意数量的接口,但接口仅声明一个签名。 So if two interfaces include functions with the same signature, fine, your class must implement a function with that signature.因此,如果两个接口包含具有相同签名的函数,那很好,您的类必须实现具有该签名的函数。 If interfaces you inherit have functions with different signatures, then the functions have nothing to do with each other, so there is no question of a conflict.如果你继承的接口有不同签名的函数,那么这些函数之间没有任何关系,所以不存在冲突的问题。

I'm not saying this is the only way.我并不是说这是唯一的方法。 C++ implements true multiple inheritance by establishing precedence rules of which implementation wins. C++ 通过建立哪个实现获胜的优先规则来实现真正的多重继承。 But the authors of Java decided to eliminate the ambiguity.但是 Java 的作者决定消除歧义。 Whether because of a philosophical belief that this made for cleaner code, or because they didn't want to do all the extra work, I don't know.是因为哲学上的信念使代码更简洁,还是因为他们不想做所有额外的工作,我不知道。

You can actually "inherit" from multiple concrete classes if they implement interfaces themselves.如果它们自己实现接口,您实际上可以从多个具体类“继承”。 innerclasses help you achieve that: innerclasses帮助您实现:

 interface IBird { public void layEgg(); } interface IMammal { public void giveMilk(); } class Bird implements IBird{ public void layEgg() { System.out.println("Laying eggs..."); } } class Mammal implements IMammal { public void giveMilk() { System.out.println("Giving milk..."); } } class Platypus implements IMammal, IBird { private class LayingEggAnimal extends Bird {} private class GivingMilkAnimal extends Mammal {} private LayingEggAnimal layingEggAnimal = new LayingEggAnimal(); private GivingMilkAnimal givingMilkAnimal = new GivingMilkAnimal(); @Override public void layEgg() { layingEggAnimal.layEgg(); } @Override public void giveMilk() { givingMilkAnimal.giveMilk(); } }

I'd like to point out something that bit me in the behind, coming from C++ where you can easily inherit many implementations too.我想指出一些在背后咬我的东西,来自 C++,您也可以轻松继承许多实现。

Having a "wide" interface with many methods means that you'll have to implement a lot of methods in your concrete classes and you can't share these easily across implementations.拥有包含许多方法的“宽”接口意味着您必须在具体类中实现许多方法,并且不能在实现之间轻松共享这些方法

For instance:例如:

interface Herbivore {
    void munch(Vegetable v);
};

interface Carnivore {
    void devour(Prey p);
}

interface AllEater : public Herbivore, Carnivore { };

class Fox implements AllEater {
   ... 
};

class Bear implements AllEater {
   ...
};

In this example, Fox and Bear cannot share a common base implementation for both it's interface methods munch and devour .在此示例中,Fox 和 Bear 无法共享其接口方法munchdevour的公共基础实现。

If the base implementations look like this, we'd maybe want to use them for Fox and Bear :如果基本实现看起来像这样,我们可能希望将它们用于FoxBear

class ForestHerbivore implements Herbivore
    void munch(Vegetable v) { ... }
};

class ForestCarnivore implements Carnivore
    void devour(Prey p) { ... }
};

But we can't inherit both of these.但是我们不能同时继承这两个。 The base implementations need to be member variables in the class and methods defined can forward to that.基本实现需要是类中的成员变量,定义的方法可以转发给它。 Ie: IE:

class Fox implements AllEater {
    private ForestHerbivore m_herbivore;
    private ForestCarnivore m_carnivore;

    void munch(Vegetable v) { m_herbivore.munch(v); }
    void devour(Prey p) { m_carnivore.devour(p); }
}

This gets unwieldy if interfaces grow (ie more than 5-10 methods...)如果接口增长(即超过 5-10 个方法......)

A better approach is to define an interface as an aggregation of interfaces:更好的方法是将接口定义为接口的聚合:

interface AllEater {
    Herbivore asHerbivore();
    Carnivore asCarnivore();
}

This means that Fox and Bear only has to implement these two methods, and the interfaces and base classes can grow independetly of the aggregate AllEater interface that concerns the implementing classes.这意味着FoxBear只需要实现这两个方法,并且接口和基类可以独立于涉及实现类的聚合AllEater接口增长。

Less coupling this way, if it works for your app.如果它适用于您的应用程序,则以这种方式减少耦合。

It's not fair to say that interfaces 'simulate' multiple inheritance.说接口“模拟”多重继承是不公平的。

Sure, your type can implement multiple interfaces and act as many different types polymorphically.当然,您的类型可以实现多个接口并以多态方式充当许多不同的类型。 However, you obviously won't inherit behaviour or implementations under this arrangement.但是,在这种安排下,您显然不会继承行为或实现。

Generally look at composition where you think you may need multiple inheritance.通常查看您认为可能需要多重继承的组合。

OR A potential solution to achieving something multiple inheritance like is the Mixin interface - http://csis.pace.edu/~bergin/patterns/multipleinheritance.html .或实现多重继承的潜在解决方案是 Mixin 接口 - http://csis.pace.edu/~bergin/patterns/multipleinheritance.html Use with care!小心使用!

They don't.他们没有。

I think that the confusion comes from people believing that implementing an interface constitutes some form of inheritance.我认为混淆来自人们相信实现接口构成某种形式的继承。 It doesn't;它没有; the implementation can simply be blank, no behavior is forced by the act or guaranteed through any contract.实施可以简单地是空白的,没有行为是由行为强制的或通过任何合同来保证的。 A typical example is the Clonable-interface, which while alluding to lots of great functionality, which defines so little that's it's essentially useless and potentially dangerous.一个典型的例子是 Clonable-interface,虽然它暗指了许多很棒的功能,但它定义的很少,以至于它基本上是无用的,而且有潜在的危险。

What do you inherit by implementing an interface?你通过实现一个接口继承了什么? Bubkes!巴克斯! So in my opinion, stop using the words interface and inheritance in the same sentence.所以在我看来,不要在同一句话中使用接口和继承这两个词。 As Michael Borgwardt said, an interface is not a definition but an aspect.正如 Michael Borgwardt 所说,接口不是一个定义,而是一个方面。

There are cases where multiple-inheritance turns to be very handy and difficult to replace with interfaces without writing more code.在某些情况下,多重继承变得非常方便,并且在不编写更多代码的情况下很难用接口替换。 For example, there are Android apps that use classes derived from Activity and others from FragmentActivity in the same app.例如,在同一个应用程序中,有些 Android 应用程序使用从 Activity 派生的类和其他从 FragmentActivity 派生的类。 If you have a particular feature you want to share in a common class, in Java you will have to duplicate code instead of let child classes of Activity and FragmentsActivity derive from the same SharedFeature class.如果您想在公共类中共享特定功能,则在 Java 中您将不得不复制代码,而不是让 Activity 和 FragmentsActivity 的子类从同一个 SharedFeature 类派生。 And the poor implementation of generics in Java doesn't help either because writing the following is illegal: Java 中泛型的糟糕实现也无济于事,因为编写以下内容是非法的:

public class SharedFeature<T> extends <T extends Activity>

...
...

There is no support for multiple inheritance in java. java中不支持多重继承。

This story of supporting multiple inheritance using interface is what we developers cooked up.这个使用接口支持多重继承的故事是我们开发人员编造的。 Interface gives flexibility than concrete classes and we have option to implement multiple interface using single class.接口比具体类更灵活,我们可以选择使用单个类实现多个接口。 This is by agreement we are adhering to two blueprints to create a class.根据协议,我们遵循两个蓝图来创建一个类。

This is trying to get closer to multiple inheritance.这是试图更接近多重继承。 What we do is implement multiple interface, here we are not extending (inheriting) anything.我们所做的是实现多个接口,这里我们不扩展(继承)任何东西。 The implementing class is the one that is going to add the properties and behavior.实现类是要添加属性和行为的类。 It is not getting the implementation free from the parent classes.它没有从父类中获得实现。 I would simply say, there is no support for multiple inheritance in java.我只想说,java 中不支持多重继承。

I don't think they do.我不认为他们这样做。

Inheritance is specifically an implementation-oriented relationship between implementations.继承具体是实现之间的面向实现的关系。 Interfaces do not provide any implementation information at all, but instead define a type.接口根本不提供任何实现信息,而是定义一个类型。 To have inheritance, you need to specifically inherit some behaviors or attributes from a parent class.要进行继承,您需要专门从父类继承一些行为或属性。

I believe there is a question here somewhere specifically about the role of interfaces and multiple inheritance, but I can't find it now...我相信这里某个地方有一个关于接口和多重继承的作用的问题,但我现在找不到了......

There's really no simulation of multiple inheritance in Java.在 Java 中确实没有模拟多重继承。

People will sometimes say that you can simulate multiple inheritance using Interfaces because you can implement more than one interface per class, and then use composition (rather than inheritance) in your class to achieve the behaviors of the multiple classes that you were trying to inherit from to begin with.人们有时会说,您可以使用接口模拟多重继承,因为您可以为每个类实现多个接口,然后在您的类中使用组合(而不是继承)来实现您试图继承的多个类的行为开始。

如果它在您的对象模型中有意义,您当然可以从一个类继承并实现 1 个或多个接口。

No, Java does not support multiple inheritance.不,Java 不支持多重继承。 Neither using class nor using interface.既不使用类也不使用接口。 Refer to this link for more info https://devsuyed.wordpress.com/2016/07/21/does-java-support-multiple-inheritance有关更多信息,请参阅此链接https://devsuyed.wordpress.com/2016/07/21/does-java-support-multiple-inheritance

I also have to say that Java doesn't support multiple inheritance.我还要说Java不支持多重继承。

You have to differentiate the meaning between extends and implements keywords in Java.您必须区分 Java 中extendsimplements关键字之间的含义。 If we use extends , we are actually inheriting the class after that keyword.如果我们使用extends ,我们实际上是在继承该关键字之后的类。 But, in order to make everything simple, we can't use extends more than once.但是,为了让一切变得简单,我们不能多次使用extends But you can implement as many Interfaces as you wish.但是您可以根据需要实现任意数量的接口。

If you implement an interface, there's a zero chance that you will miss the implementation of all the methods in each interface (Exception: default implementations of interface methods introduced in Java 8) So, you are now fully aware of what is happening with the things that you have embedded to your fresh class.如果您实现了一个接口,那么您将错过每个接口中所有方法的实现的可能性为零(例外:Java 8 中引入的接口方法的默认实现)因此,您现在完全了解事物发生的情况你已经嵌入到你的新课程中。

Why Java doesn't allow multiple inheritance is actually, multiple inheritance makes the code somewhat complex. Java 不允许多重继承的原因实际上是,多重继承使代码有些复杂。 Sometimes, two methods of parent classes might conflict due to having the same signatures.有时,由于具有相同的签名,父类的两个方法可能会发生冲突。 But if you are forced to implement all the methods manually, you will get the full understanding about what's going on, as I mentioned above.但是,如果您被迫手动实现所有方法,您将完全了解正在发生的事情,正如我上面提到的。 It makes your code more understandable to you.它使您的代码更容易理解。

If you need more info on Java interfaces, check out this article,http://www.geek-programmer.com/introduction-to-java-interfaces/如果您需要有关 Java 接口的更多信息,请查看这篇文章,http://www.geek-programmer.com/introduction-to-java-interfaces/

Between two Java class multiple Inheritance directly is not possible.两个Java类之间直接多重继承是不可能的。 In this case java recommend Use to interface and declare method inside interface and implement method with Child class.在这种情况下,java 推荐使用接口和声明接口内的方法并使用 Child 类实现方法。

interface ParentOne{
   public String parentOneFunction();
}

interface ParentTwo{
    public String parentTwoFunction();
}

class Child implements ParentOne,ParentTwo{

   @Override
    public String parentOneFunction() {
        return "Parent One Finction";
    }

    @Override
    public String parentTwoFunction() {
        return "Parent Two Function";
    }

    public String childFunction(){
       return  "Child Function";
    }
}
public class MultipleInheritanceClass {
    public static void main(String[] args) {
        Child ch = new Child();
        System.out.println(ch.parentOneFunction());
        System.out.println(ch.parentTwoFunction());
        System.out.println(ch.childFunction());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM