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

后端 未结 10 1002
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  猫巷女王i
    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.

提交回复
热议问题