Java overloading and overriding

后端 未结 9 1632
暖寄归人
暖寄归人 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

    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.
    

提交回复
热议问题