Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Polymorphism , Overloading and Overriding similar concepts? [closed]

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

like image 943
Ammar Raja Avatar asked Oct 15 '12 10:10

Ammar Raja


People also ask

Is polymorphism closely associated to the concept of method overriding and method overloading?

Polymorphism can be achieved through overriding . As already mentioned above, polymorphism refers to the ability of an object to provide different behaviours (use different implementations) depending on its own nature.

Is polymorphism and overriding the same?

Overriding is when you call a method on an object and the method in the subclass with the same signature as the one in the superclass is called. Polymorphism is where you are not sure of the objects type at runtime and the most specific method is called.

Is overriding and overloading both polymorphism?

Overriding implements Runtime Polymorphism whereas Overloading implements Compile time polymorphism. The method Overriding occurs between superclass and subclass. Overloading occurs between the methods in the same class.

Which is closely associated to the concept of method overriding and method overloading?

Answer. Answer: Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters (i.e., method signature).


1 Answers

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 Strings and addition with numeric operands).

The example in Wikipedia is quite illustrative.

The following related questions might be also useful:

  • Polymorphism vs Overriding vs Overloading
  • Polymorphism - Define In Just Two Sentences
like image 69
Xavi López Avatar answered Sep 23 '22 17:09

Xavi López