Java Multiple Inheritance

前端 未结 17 1285
猫巷女王i
猫巷女王i 2020-11-22 09:36

In an attempt to fully understand how to solve Java\'s multiple inheritance problems I have a classic question that I need clarified.

Lets say I have class Ani

相关标签:
17条回答
  • 2020-11-22 10:11

    In Java 8, which is still in the development phase as of February 2014, you could use default methods to achieve a sort of C++-like multiple inheritance. You could also have a look at this tutorial which shows a few examples that should be easier to start working with than the official documentation.

    0 讨论(0)
  • 2020-11-22 10:17

    Technically speaking, you can only extend one class at a time and implement multiple interfaces, but when laying hands on software engineering, I would rather suggest a problem specific solution not generally answerable. By the way, it is good OO practice, not to extend concrete classes/only extend abstract classes to prevent unwanted inheritance behavior - there is no such thing as an "animal" and no use of an animal object but only concrete animals.

    0 讨论(0)
  • 2020-11-22 10:19

    you can have an interface hierarchy and then extend your classes from selected interfaces :

    public interface IAnimal {
    }
    
    public interface IBird implements IAnimal {
    }
    
    public  interface IHorse implements IAnimal {
    }
    
    public interface IPegasus implements IBird,IHorse{
    }
    

    and then define your classes as needed, by extending a specific interface :

    public class Bird implements IBird {
    }
    
    public class Horse implements IHorse{
    }
    
    public class Pegasus implements IPegasus {
    }
    
    0 讨论(0)
  • 2020-11-22 10:20

    There are two fundamental approaches to combining objects together:

    • The first is Inheritance. As you have already identified the limitations of inheritance mean that you cannot do what you need here.
    • The second is Composition. Since inheritance has failed you need to use composition.

    The way this works is that you have an Animal object. Within that object you then add further objects that give the properties and behaviors that you require.

    For example:

    • Bird extends Animal implements IFlier
    • Horse extends Animal implements IHerbivore, IQuadruped
    • Pegasus extends Animal implements IHerbivore, IQuadruped, IFlier

    Now IFlier just looks like this:

     interface IFlier {
         Flier getFlier();
     }
    

    So Bird looks like this:

     class Bird extends Animal implements IFlier {
          Flier flier = new Flier();
          public Flier getFlier() { return flier; }
     }
    

    Now you have all the advantages of Inheritance. You can re-use code. You can have a collection of IFliers, and can use all the other advantages of polymorphism, etc.

    However you also have all the flexibility from Composition. You can apply as many different interfaces and composite backing class as you like to each type of Animal - with as much control as you need over how each bit is set up.

    Strategy Pattern alternative approach to composition

    An alternative approach depending on what and how you are doing is to have the Animal base class contain an internal collection to keep the list of different behaviors. In that case you end up using something closer to the Strategy Pattern. That does give advantages in terms of simplifying the code (for example Horse doesn't need to know anything about Quadruped or Herbivore) but if you don't also do the interface approach you lose a lot of the advantages of polymorphism, etc.

    0 讨论(0)
  • 2020-11-22 10:23
    1. Define interfaces for defining the capabilities. You can define multiple interfaces for multiple capabilities. These capabilities can be implemented by specific Animal or Bird.
    2. Use inheritance to establish relationships among classes by sharing non-static and non-public data/methods.
    3. Use Decorator_pattern to add capabilities dynamically. This will allow you to reduce number of inheritance classes & combinations.

    Have a look at below example for better understanding

    When to Use the Decorator Pattern?

    0 讨论(0)
  • 2020-11-22 10:24

    It is safe to keep a horse in a stable with a half door, as a horse cannot get over a half door. Therefore I setup a horse housing service that accepts any item of type horse and puts it in a stable with a half door.

    So is a horse like animal that can fly even a horse?

    I used to think a lot about multiple inheritance, however now that I have been programming for over 15 years, I no longer care about implementing multiple inheritance.

    More often than not, when I have tried to cope with a design that pointed toward multiple inheritance, I have later come to release that I had miss understood the problem domain.

    OR

    If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction.

    0 讨论(0)
提交回复
热议问题