Java overloading and overriding

后端 未结 9 1611
暖寄归人
暖寄归人 2020-11-29 08:16

We always say that method overloading is static polymorphism and overriding is runtime polymorphism. What exactly do we mean by static here? Is the call to a method resolved

相关标签:
9条回答
  • 2020-11-29 09:07

    Method Overloading simply means providing two separate methods in a class with the same name but different arguments while method return type may or may not be different which allows us to reuse the same method name.

    But both methods are different hence can be resolved by compiler at compile time that's is why it is also known as Compile Time Polymorphism or Static Polymorphism

    Method Overriding means defining a method in the child class which is already defined in the parent class with same method signature i.e same name, arguments and return type.

    Mammal mammal = new Cat();
    System.out.println(mammal.speak());
    

    At the line mammal.speak() compiler says the speak() method of reference type Mammal is getting called, so for compiler this call is Mammal.speak().

    But at the execution time JVM knows clearly that mammal reference is holding the reference of object of Cat, so for JVM this call is Cat.speak().

    Because method call is getting resolved at runtime by JVM that's why it is also known as Runtime Polymorphism and Dynamic Method Dispatch.

    Difference Between Method Overloading and Method Overriding

    For more details, you can read Everything About Method Overloading Vs Method Overriding.

    0 讨论(0)
  • 2020-11-29 09:07

    Tried to cover all differences

                           Overloading                          Overriding
    
    Method Name            Must be same                         Must be same
    
    Argument Types         Must be same                         Must be different
    
    
    Return Type            No restriction                       Must be same till 1.4V 
                                                                but after 1.4V 
                                                                co- variants 
                                                                were introduced
    
    private/static/final   Can be overloaded                    Cannot be overridden
    
    Access Modifiers       No restriction                       Cannot reduce the scope
    
    Throws keyword         No restriction                       If child class method 
                                                                throws a checked 
                                                                exception the parent 
                                                                class method must throw 
                                                                the same or the  
                                                                parent exception
    
    Method Resolution      Taken care by compiler               Taken care by JVM based 
                           based on reference types             on run-time object
    
    Known as               Compile-Time Polymorphism,           RunTime Polymorphism, 
                           Static Polymorphism, or              dynamic polymorphism,
                           early binding                        late binding.
    
    0 讨论(0)
  • 2020-11-29 09:09

    Simple Definition - Method overloading deals with the notion of having two or more methods(functions) in the same class with the same name but different arguments.

    While Method overriding means having two methods with the same arguments, but different implementation. One of them would exist in the Parent class (Base Class) while another will be in the derived class(Child Class).@Override annotation is required for this.

    Check this : Click here for a detailed example

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