Polymorphism vs Overriding vs Overloading

前端 未结 21 2811
北恋
北恋 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:27

    I think guys your are mixing concepts. Polymorphism is the ability of an object to behave differently at run time. For achieving this, you need two requisites:

    1. Late Binding
    2. Inheritance.

    Having said that overloading means something different to overriding depending on the language you are using. For example in Java does not exist overriding but overloading. Overloaded methods with different signature to its base class are available in the subclass. Otherwise they would be overridden (please, see that I mean now the fact of there is no way to call your base class method from outside the object).

    However in C++ that is not so. Any overloaded method, independently whether the signature is the same or not (diffrrent amount, different type) is as well overridden. That is to day, the base class' method is no longer available in the subclass when being called from outside the subclass object, obviously.

    So the answer is when talking about Java use overloading. In any other language may be different as it happens in c++

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

    You are correct that overloading is not the answer.

    Neither is overriding. Overriding is the means by which you get polymorphism. Polymorphism is the ability for an object to vary behavior based on its type. This is best demonstrated when the caller of an object that exhibits polymorphism is unaware of what specific type the object is.

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

    Polymorphism means more than one form, same object performing different operations according to the requirement.

    Polymorphism can be achieved by using two ways, those are

    1. Method overriding
    2. Method overloading

    Method overloading means writing two or more methods in the same class by using same method name, but the passing parameters is different.

    Method overriding means we use the method names in the different classes,that means parent class method is used in the child class.

    In Java to achieve polymorphism a super class reference variable can hold the sub class object.

    To achieve the polymorphism every developer must use the same method names in the project.

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

    Polymorphism relates to the ability of a language to have different object treated uniformly by using a single interfaces; as such it is related to overriding, so the interface (or the base class) is polymorphic, the implementor is the object which overrides (two faces of the same medal)

    anyway, the difference between the two terms is better explained using other languages, such as c++: a polymorphic object in c++ behaves as the java counterpart if the base function is virtual, but if the method is not virtual the code jump is resolved statically, and the true type not checked at runtime so, polymorphism include the ability for an object to behave differently depending on the interface used to access it; let me make an example in pseudocode:

    class animal {
        public void makeRumor(){
            print("thump");
        }
    }
    class dog extends animal {
        public void makeRumor(){
            print("woff");
        }
    }
    
    animal a = new dog();
    dog b = new dog();
    
    a.makeRumor() -> prints thump
    b.makeRumor() -> prints woff
    

    (supposing that makeRumor is NOT virtual)

    java doesn't truly offer this level of polymorphism (called also object slicing).

    animal a = new dog(); dog b = new dog();

    a.makeRumor() -> prints thump
    b.makeRumor() -> prints woff
    

    on both case it will only print woff.. since a and b is refering to class dog

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

    Polymorphism is more likely as far as it's meaning is concerned ... to OVERRIDING in java

    It's all about different behavior of the SAME object in different situations(In programming way ... you can call different ARGUMENTS)

    I think the example below will help you to understand ... Though it's not PURE java code ...

         public void See(Friend)
         {
            System.out.println("Talk");
         }
    

    But if we change the ARGUMENT ... the BEHAVIOR will be changed ...

         public void See(Enemy)
         {
            System.out.println("Run");
         }
    

    The Person(here the "Object") is same ...

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

    overloading is when you define 2 methods with the same name but different parameters

    overriding is where you change the behavior of the base class via a function with the same name in a subclass.

    So Polymorphism is related to overriding but not really overloading.

    However if someone gave me a simple answer of "overriding" for the question "What is polymorphism?" I would ask for further explanation.

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