Polymorphism vs Overriding vs Overloading

前端 未结 21 2809
北恋
北恋 2020-11-22 01:10

In terms of Java, when someone asks:

what is polymorphism?

Would overloading or overriding be

相关标签:
21条回答
  • 2020-11-22 01:10

    Specifically saying overloading or overriding doesn't give the full picture. Polymorphism is simply the ability of an object to specialize its behavior based on its type.

    I would disagree with some of the answers here in that overloading is a form of polymorphism (parametric polymorphism) in the case that a method with the same name can behave differently give different parameter types. A good example is operator overloading. You can define "+" to accept different types of parameters -- say strings or int's -- and based on those types, "+" will behave differently.

    Polymorphism also includes inheritance and overriding methods, though they can be abstract or virtual in the base type. In terms of inheritance-based polymorphism, Java only supports single class inheritance limiting it polymorphic behavior to that of a single chain of base types. Java does support implementation of multiple interfaces which is yet another form of polymorphic behavior.

    0 讨论(0)
  • 2020-11-22 01:13

    Polymorphism is a multiple implementations of an object or you could say multiple forms of an object. lets say you have class Animals as the abstract base class and it has a method called movement() which defines the way that the animal moves. Now in reality we have different kinds of animals and they move differently as well some of them with 2 legs, others with 4 and some with no legs, etc.. To define different movement() of each animal on earth, we need to apply polymorphism. However, you need to define more classes i.e. class Dogs Cats Fish etc. Then you need to extend those classes from the base class Animals and override its method movement() with a new movement functionality based on each animal you have. You can also use Interfaces to achieve that. The keyword in here is overriding, overloading is different and is not considered as polymorphism. with overloading you can define multiple methods "with same name" but with different parameters on same object or class.

    0 讨论(0)
  • 2020-11-22 01:15

    Here's an example of polymorphism in pseudo-C#/Java:

    class Animal
    {
        abstract string MakeNoise ();
    }
    
    class Cat : Animal {
        string MakeNoise () {
            return "Meow";
        }
    }
    
    class Dog : Animal {
        string MakeNoise () {
            return "Bark";
        }
    }
    
    Main () {
       Animal animal = Zoo.GetAnimal ();
       Console.WriteLine (animal.MakeNoise ());
    }
    

    The Main function doesn't know the type of the animal and depends on a particular implementation's behavior of the MakeNoise() method.

    Edit: Looks like Brian beat me to the punch. Funny we used the same example. But the above code should help clarify the concepts.

    0 讨论(0)
  • 2020-11-22 01:15

    The term overloading refers to having multiple versions of something with the same name, usually methods with different parameter lists

    public int DoSomething(int objectId) { ... }
    public int DoSomething(string objectName) { ... }
    

    So these functions might do the same thing but you have the option to call it with an ID, or a name. Has nothing to do with inheritance, abstract classes, etc.

    Overriding usually refers to polymorphism, as you described in your question

    0 讨论(0)
  • 2020-11-22 01:18

    Polymorphism is the ability of a class instance to behave as if it were an instance of another class in its inheritance tree, most often one of its ancestor classes. For example, in Java all classes inherit from Object. Therefore, you can create a variable of type Object and assign to it an instance of any class.

    An override is a type of function which occurs in a class which inherits from another class. An override function "replaces" a function inherited from the base class, but does so in such a way that it is called even when an instance of its class is pretending to be a different type through polymorphism. Referring to the previous example, you could define your own class and override the toString() function. Because this function is inherited from Object, it will still be available if you copy an instance of this class into an Object-type variable. Normally, if you call toString() on your class while it is pretending to be an Object, the version of toString which will actually fire is the one defined on Object itself. However, because the function is an override, the definition of toString() from your class is used even when the class instance's true type is hidden behind polymorphism.

    Overloading is the action of defining multiple methods with the same name, but with different parameters. It is unrelated to either overriding or polymorphism.

    0 讨论(0)
  • 2020-11-22 01:18

    Neither:

    Overloading is when you have the same function name that takes different parameters.

    Overriding is when a child class replaces a parent's method with one of its own (this in iteself does not constitute polymorphism).

    Polymorphism is late binding, e.g. the base class (parent) methods are being called but not until runtime does the application know what the actual object is - it may be a child class whose methods are different. This is because any child class can be used where a base class is defined.

    In Java you see polymorphism a lot with the collections library:

    int countStuff(List stuff) {
      return stuff.size();
    }
    

    List is the base class, the compiler has no clue if you're counting a linked list, vector, array, or a custom list implementation, as long as it acts like a List:

    List myStuff = new MyTotallyAwesomeList();
    int result = countStuff(myStuff);
    

    If you were overloading you'd have:

    int countStuff(LinkedList stuff) {...}
    int countStuff(ArrayList stuff) {...}
    int countStuff(MyTotallyAwesomeList stuff) {...}
    etc...
    

    and the correct version of countStuff() would be picked by the compiler to match the parameters.

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