Polymorphism VS Overriding VS Overloading

Polymorphism vs Overriding vs Overloading

The clearest way to express polymorphism is via an abstract base class (or interface)

public abstract class Human{
...
public abstract void goPee();
}

This class is abstract because the goPee() method is not definable for Humans. It is only definable for the subclasses Male and Female. Also, Human is an abstract concept — You cannot create a human that is neither Male nor Female. It’s got to be one or the other.

So we defer the implementation by using the abstract class.

public class Male extends Human{
...
@Override
public void goPee(){
System.out.println("Stand Up");
}
}

and

public class Female extends Human{
...
@Override
public void goPee(){
System.out.println("Sit Down");
}
}

Now we can tell an entire room full of Humans to go pee.

public static void main(String[] args){
ArrayList<Human> group = new ArrayList<Human>();
group.add(new Male());
group.add(new Female());
// ... add more...

// tell the class to take a pee break
for (Human person : group) person.goPee();
}

Running this would yield:

Stand Up
Sit Down
...

OverRiding Vs PolyMorphism

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. Therefore the behaviour of the method called may differ, depending on the objects type at runtime.

Overriding is a type of polymorphism along with overloading and dynamic (late) binding. You can see more details here about the different types.

Are Polymorphism , Overloading and Overriding similar concepts?

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

Is Method Overloading considered polymorphism?

"Polymorphism" is just a word and doesn't have a globally agreed-upon, precise definition. You will not be enlightened by either a "yes" or a "no" answer to your question because the difference will be in the chosen definition of "polymorphism" and not in the essence of method overloading as a feature of any particular language. You can see the evidence of that in most of the other answers here, each introducing its own definition and then evaluating the language feature against it.

Polymorphism - Overloading/Overriding

To answer your questions:

  1. It's the ability to select more specialized methods in runtime depending on the object being used to call it.
  2. Of course. Polymorphism could occur with no abstract classes involved.
  3. No, overloading/overriding are not types of polymorphism.

Here's an example with polymorphism happening with no abstract classes involved.

// non abstract
class A
{
public void a()
{
System.out.println("Hello from A");
}
}

class B
extends A
{
@Override
public void a()
{
System.out.println("Hello from B");
}
}

class C
{
public static void SomeStatic(A a)
{
// HERE IS WHERE POLYMORPHISM OCCUR
a.a();
}
}

Polymorphism in class C occurs because SomeStatic method could be call with a Reference to A object or a Reference to B object. If it's called with a reference to A, A's a method will be called. If it's called with a reference to B, B's a method will be called. This ability of changing, on runtime, the actual method being called is called Polymorphism.

Overloading barely has anything to do with Polymorphism. In fact, you can hace overloading with no inheritance involved if you want. You could even have overloading with no object orientation involved. Overloading is just letting two function to exist with the same name but with different parameters.

Overriding on the other hand, is just re-defining a method on a specialized (inherited) class. Overriding a method is necessary for polymorphism to happen. Otherwise, the would be no DUAL POSSIBILITIES on runtime (take a close look at the example).

Class C is the key to understand it all:

class Main
{
public static void main(String[] args)
{
A a = new A();
B b = new B();
C.SomeStatic(a); // will call A's a
C.SomeStatic(b); // will call B's a
// AND THIS IS POLYMORPHISM
// C will decide WHICH METHOD TO CALL
// only at runtime
}
}

Poly: comes from greek. Means many.
Morph: comes from greek. Means form.

So in Polymorphism there are "many" (poly) "forms" (morph) of calling a method. Which one will be called, depends on the object being used to call the method.

Polymorphism - Method overriding and overloading not clear

Important: I am assuming that class BTW extends Car not Audi (which would makes no sense IMO).


But with same object i am not able to call first(int x, int y) of Audi class.

You need to distinguish between variable type and value (object) type. In case of

Car car = new Audi();

variable type is Car while type of object it holds is Audi.

You need to realize that compiler doesn't assume what is the value of variable. It applies same rules as if it was parameter of method like

void someMethod(Car car){
//do something with `car`
}

where inside of that method we don't know if it will be used with someMethod(new Audi()); or someMethod(new BMW());.

So which methods can be safely invoked via car variable inside someMethod body? Only those which are guaranteed to be implemented (to appear) in all objects which can be used as method arguments. If that method would let us write car.first(1, 2); it will work for scenario like someMethod(new Audi()) but will fail for someMethod(new BMW()) because BMW doesn't have first(int x, int y) method.



Related Topics



Leave a reply



Submit