Saturday, April 8, 2017

Polymorphism Comparison


Below list summarizes differences between method overidding and overloading.

Argument list
Overriding: Same method name as well as same data type, number, sequence
Overloading: Same method name but different method signature (data type, number, sequence)

Return type
Overriding: Same or compatible as subtype
Overloading: Same or different (doesn't matter)

Polymorphism type
Overridding: Runtime, object type (not reference variable type) determines which methods should be overridden at runtime
Overloading: Compile time

Binding
Overridding: dynamic => same method signatures make compiler confuse
Overloading: static => private, static and final are restricted to local class scope

Applies to
Overridding: inherited and abstract methods, static methods can be redeclared in subclass but nothing to do with static method in parent class
Overloading: in the same class or subclass, constructor, private, static and final methods

Exceptions
Overridding: can throw any unchecked exceptions. However, the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method.
Overloading: can throw different exceptions

Performance
Overridding: Because done by runtime
Overloading: Better

Class Requirement
Overridding: Two classes (superclass and subclass)
Overloading: within one class

Access Level
Overridding: cannot be restrictive than overridden method of parent class
Overloading: can be restrictive or more accessible

No comments:

Post a Comment

Abstraction vs Interface

Abstract Interface method abstract and concrete abstract only, support default and static method ...