what's the difference between inheritance and polymorphism?

前端 未结 8 1807
[愿得一人]
[愿得一人] 2020-12-05 05:08

can you give me a simple example of inheritance and polymorphism, so it could be fully clear and understandable?

using C# would make it more clear, as I already lear

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 05:48

    It's all about behaviours.

    A class declares a certain behaviour (interface or contract):

    That class may also define that behaviour (implementation) or delegate either the whole or part of it to any of its subclasses:

    In pseudocode:

    class Animal {
        method walk()
        method speak()
        method jump()
        // ... here goes the implementation of the methods
    }
    

    Through subclassing you make a class inherit another class' behaviour.

    When the implementation of a method is delegated to subclasses, the method is called abstract in the base class and, in languages like Java, the whole base class becomes abstract as well:

    abstract class Animal {
        method walk() {
           doWalk()
        }
        method speak() {
           print "hi, I am an animal!"
        }
        abstract method jump() // delegated to specific animals
    }
    
    class Horse inherits from Animal {
        override method walk() {
            doWalkLikeAHorse()
        }
        override method speak() {
            print "hi, I am a horse!"
        }
        override method jump() { 
            doJumpLikeAHorse()
        }
    }
    
    class Elephant inherits from Animal {
        override method walk() {
            doWalkLikeAnElephant()
        }
        override method speak() {
            print "hi, I am an elephant!"
        }
        override method jump() { 
            throw error "Sorry, I can't jump!!"
        } 
    }
    

    A class' behaviour is by default virtual, which means that any class methods may be overridden by any subclasses. This is how it works in languages like C# and Java, but not necessarily in C++, for example.

    In substance, the behaviour of a base class is only virtual and may assume "multiple" (poly) "different forms" (morphs) when subclasses override that virtual behaviour. That's why it's called polymorphism. In pseudocode:

    function makeAnimalSpeak(Animal animal) {
        animal.speak();
    }
    
    makeAnimalSpeak(new Elephant()) // output: "hi, I am an elephant"
    makeAnimalSpeak(new Horse())  // output: "hi, I am a horse"
    

    Other people have offered you better examples here.

    In languages like C# and Java you have the idea of interface (which does not exist in C++), which is just a declaration of a behaviour. An interface, unlike a class, has no obligation to implement a behaviour. It's just a declaration. Any class may implement that behaviour, no matter what base class they inherit from. In pseudocode:

    interface FlyingBeing {
        method fly()
    }
    
    class FlyingPig inherits from Animal implements FlyingBeing {
        method fly() {
           print "hey, look at me, I am a flying pig!!"
        }
    }
    

提交回复
热议问题