What is polymorphism, what is it for, and how is it used?

前端 未结 28 2697
南笙
南笙 2020-11-21 07:08

What is polymorphism, what is it for, and how is it used?

相关标签:
28条回答
  • 2020-11-21 07:40

    Polymorphism is this:

    class Cup {
       int capacity
    }
    
    class TeaCup : Cup {
       string flavour
    }
    
    class CoffeeCup : Cup {
       string brand
    }
    
    Cup c = new CoffeeCup();
    
    public int measure(Cup c) {
        return c.capacity
    }
    

    you can pass just a Cup instead of a specific instance. This aids in generality because you don't have to provide a specific measure() instance per each cup type

    0 讨论(0)
  • 2020-11-21 07:40

    I know this is an older question with a lot of good answers but I'd like to include a one sentence answer:

    Treating a derived type as if it were it's base type.

    There are plenty of examples above that show this in action, but I feel this is a good concise answer.

    0 讨论(0)
  • 2020-11-21 07:40

    Polymorphism literally means, multiple shapes. (or many form) : Object from different classes and same name method , but workflows are different. A simple example would be:

    Consider a person X.

    He is only one person but he acts as many. You may ask how:

    He is a son to his mother. A friend to his friends. A brother to his sister.

    0 讨论(0)
  • 2020-11-21 07:41

    Usually this refers the the ability for an object of type A to behave like an object of type B. In object oriented programming this is usually achieve by inheritance. Some wikipedia links to read more:

    • Polymorphism in object oriented programming
    • Type polymorphism

    EDIT: fixed broken links.

    0 讨论(0)
  • 2020-11-21 07:42

    I've provided a high-level overview of polymorphism for another question:

    Polymorphism in c++

    Hope it helps. An extract...

    ...it helps to start from a simple test for it and definition of [polymorphism]. Consider the code:

    Type1 x;
    Type2 y;
    
    f(x);
    f(y);
    

    Here, f() is to perform some operation and is being given the values x and y as inputs. To be polymorphic, f() must be able to operate with values of at least two distinct types (e.g. int and double), finding and executing type-appropriate code.

    ( continued at Polymorphism in c++ )

    0 讨论(0)
  • 2020-11-21 07:44

    Polymorphism in OOP means a class could have different types, inheritance is one way of implementing polymorphism.

    for example, Shape is an interface, it has Square, Circle, Diamond subtypes. now you have a Square object, you can upcasting Square to Shape automatically, because Square is a Shape. But when you try to downcasting Shape to Square, you must do explicit type casting, because you can't say Shape is Square, it could be Circle as well. so you need manually cast it with code like Square s = (Square)shape, what if the shape is Circle, you will get java.lang.ClassCastException, because Circle is not Square.

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