Java Multiple Inheritance

前端 未结 17 1267
猫巷女王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:32

    You could create interfaces for animal classes (class in the biological meaning), such as public interface Equidae for horses and public interface Avialae for birds (I'm no biologist, so the terms may be wrong).

    Then you can still create a

    public class Bird implements Avialae {
    }
    

    and

    public class Horse implements Equidae {}
    

    and also

    public class Pegasus implements Avialae, Equidae {}
    

    Adding from the comments:

    In order to reduce duplicate code, you could create an abstract class that contains most of the common code of the animals you want to implement.

    public abstract class AbstractHorse implements Equidae {}
    
    public class Horse extends AbstractHorse {}
    
    public class Pegasus extends AbstractHorse implements Avialae {}
    

    Update

    I'd like to add one more detail. As Brian remarks, this is something the OP already knew.

    However, I want to emphasize, that I suggest to bypass the "multi-inheritance" problem with interfaces and that I don't recommend to use interfaces that represent already a concrete type (such as Bird) but more a behavior (others refer to duck-typing, which is good, too, but I mean just: the biological class of birds, Avialae). I also don't recommend to use interface names starting with a capital 'I', such as IBird, which just tells nothing about why you need an interface. That's the difference to the question: construct the inheritance hierarchy using interfaces, use abstract classes when useful, implement concrete classes where needed and use delegation if appropriate.

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

    Ehm, your class can be the subclass for only 1 other, but still, you can have as many interfaces implemented, as you wish.

    A Pegasus is in fact a horse (it is a special case of a horse), which is able to fly (which is the "skill" of this special horse). From the other hand, you can say, the Pegasus is a bird, which can walk, and is 4legged - it all depends, how it is easier for you to write the code.

    Like in your case you can say:

    abstract class Animal {
       private Integer hp = 0; 
       public void eat() { 
          hp++; 
       }
    }
    interface AirCompatible { 
       public void fly(); 
    }
    class Bird extends Animal implements AirCompatible { 
       @Override
       public void fly() {  
           //Do something useful
       }
    } 
    class Horse extends Animal {
       @Override
       public void eat() { 
          hp+=2; 
       }
    
    }
    class Pegasus extends Horse implements AirCompatible {
       //now every time when your Pegasus eats, will receive +2 hp  
       @Override
       public void fly() {  
           //Do something useful
       }
    }
    
    0 讨论(0)
  • 2020-11-22 10:34

    I have a stupid idea:

    public class Pegasus {
        private Horse horseFeatures; 
        private Bird birdFeatures; 
    
       public Pegasus(Horse horse, Bird bird) {
         this.horseFeatures = horse;
         this.birdFeatures = bird;
       }
    
      public void jump() {
        horseFeatures.jump();
      }
    
      public void fly() {
        birdFeatures.fly();
      }
    }
    
    0 讨论(0)
  • 2020-11-22 10:35

    To reduce the complexity and simplify the language, multiple inheritance is not supported in java.

    Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.

    Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.

    class A {  
        void msg() {
            System.out.println("From A");
        }  
    }
    
    class B {  
        void msg() {
            System.out.println("From B");
        }  
    }
    
    class C extends A,B { // suppose if this was possible
        public static void main(String[] args) {  
            C obj = new C();  
            obj.msg(); // which msg() method would be invoked?  
        }
    } 
    
    0 讨论(0)
  • 2020-11-22 10:37

    Problem not solved. To sufficiently model this out and to prevent code replication you'd either need multiple inheritance or mixins. Interfaces with default functions are not sufficient because you cannot hold members in interfaces. Interface modeling leads to code replication in subclasses or statics, which is both evil.

    All you can do is to use a custom construction and split it up in more components and compose it all together...

    toy language

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