I am very confused about the concepts of polymorphism ,overloading and overriding because it seems same to me. Please explain these concepts, and how are they different from each other
Very confused so please guide me properly.
Thanks
I am very confused about the concepts of polymorphism ,overloading and overriding because it seems same to me. Please explain these concepts, and how are they different from each other
Very confused so please guide me properly.
Thanks
Polymorphism can be achieved through overriding. Put in short words, polymorphism refers to the ability of an object to provide different behaviors (use different implementations) depending on its own nature. Specifically, depending on its position in the class hierarchy.
Method Overriding is when a method defined in a superclass or interface is re-defined by one of its subclasses, thus modifying/replacing the behavior the superclass provides. The decision to call an implementation or another is dynamically taken at runtime, depending on the object the operation is called from. Notice the signature of the method remains the same when overriding.
Method Overloading is unrelated to polymorphism. It refers to defining different forms of a method (usually by receiving different parameter number or types). It can be seen as static polymorphism. The decision to call an implementation or another is taken at coding time. Notice in this case the signature of the method must change.
Operator overloading is a different concept, related to polymorphism, which refers to the ability of a certain language-dependant operator to behave differently based on the type of its operands (for instance, +
could mean concatenation with String
s and addition with numeric operands).
The example in Wikipedia is quite illustrative.
The following related questions might be also useful:
Shortly, no they are not the same.
Overloading means creating methods with same name but different parameters.
Overriding means re-defining body of a method of superclass in a subclass to change behavior of a method.
Polymorphism is a wide concept which includes overriding and overloading and much more in it's scope. Wikipedia's description of polymorphism can help you understand the polymorphism better. Especially the Subtype polymorphism (or inclusion polymorphism) section is where you should look.