How can interfaces replace the need for multiple inheritance when have existing classes

后端 未结 10 944
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 00:48

First of all... Sorry for this post. I know that there are many many posts on stackoverflow which are discussing multiple inheritance. But I already know that Java does not

相关标签:
10条回答
  • 2020-11-29 01:25

    Well using Interface and single base class you are simply stating:

      A) One object can be of only one type (Which is true in real life if you think , A pigeon is a bird, a toyota is a car , etc .. A pigeon is also an animal but every bird is animal anyway, so its hierarchically above the bird type -And in your OOP design Animal class should be base of Bird class in case you need to represent it -) and

      B) can do many different things (A bird can sing, can fly . A car can run , can stop ,etc..)
    which also fits the real life objects.

    In a world where objects can be of multiple types (horizontally) Let's say a a dolphin is a mammal and also a sea animal, in this case multiple inheritance would make more sense. It would be easier to represent it using multiple inheritance.

    0 讨论(0)
  • 2020-11-29 01:27

    Similar to what Andreas_D suggested but with the use of inner classes. This way you indeed extend each class and can override it in your own code if desired.

    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();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 01:27

    one possible way;

    1- You can create base class(es) for common functionality, make it abstract if you dont need to instantiate it.

    2- Create interfaces and implement those interfaces in those base class(es). If specific implementation is needed, make the method abstract. each concrete class can have its own impl.

    3- extend the abstract base class for in concrete class(es) and implement specific interfaces at this level as well

    0 讨论(0)
  • 2020-11-29 01:30

    You should probably favor composition (and delegation) over inheritance :

    public interface TaggedInterface {
        void foo();
    }
    
    public interface XMLElementInterface {
        void bar();
    }
    
    public class Tagged implements TaggedInterface {
        // ...
    }
    
    public class XMLElement implements XMLElementInterface {
        // ...
    }
    
    public class TaggedXmlElement implements TaggedInterface, XMLElementInterface {
        private TaggedInterface tagged;
        private XMLElementInterface xmlElement;
    
        public TaggedXmlElement(TaggedInterface tagged, XMLElementInterface xmlElement) {
            this.tagged = tagged;
            this.xmlElement = xmlElement;
        }
    
        public void foo() {
            this.tagged.foo();
        }
    
        public void bar() {
            this.xmlElement.bar();
        }
    
        public static void main(String[] args) {
            TaggedXmlElement t = new TaggedXmlElement(new Tagged(), new XMLElement());
            t.foo();
            t.bar();
        }
    }
    
    0 讨论(0)
提交回复
热议问题