Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphism vs Overriding vs Overloading

In terms of Java, when someone asks:

what is polymorphism?

Would overloading or overriding be an acceptable answer?

I think there is a bit more to it than that.

IF you had a abstract base class that defined a method with no implementation, and you defined that method in the sub class, is that still overridding?

I think overloading is not the right answer for sure.

like image 976
Brian G Avatar asked Sep 30 '08 19:09

Brian G


People also ask

What is the difference between polymorphism overloading and overriding?

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.

Is polymorphism and overloading same?

Polymorphism means more than one form, same object performing different operations according to the requirement. Method overloading means writing two or more methods in the same class by using same method name, but the passing parameters is different.

What is the difference between overriding vs overloading?

What is Overloading and Overriding? When two or more methods in the same class have the same name but different parameters, it's called Overloading. When the method signature (name and parameters) are the same in the superclass and the child class, it's called Overriding.

Is overloading part of polymorphism?

Overloading is a form of polymorphism (parametric polymorphism) in the case that a method with the same name can behave differently given different parameter types. Overloading is when you have the same function name that takes different parameters.


2 Answers

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 ... 
like image 162
Chris Cudmore Avatar answered Oct 18 '22 15:10

Chris Cudmore


Polymorphism is the ability of a class instance to behave as if it were an instance of another class in its inheritance tree, most often one of its ancestor classes. For example, in Java all classes inherit from Object. Therefore, you can create a variable of type Object and assign to it an instance of any class.

An override is a type of function which occurs in a class which inherits from another class. An override function "replaces" a function inherited from the base class, but does so in such a way that it is called even when an instance of its class is pretending to be a different type through polymorphism. Referring to the previous example, you could define your own class and override the toString() function. Because this function is inherited from Object, it will still be available if you copy an instance of this class into an Object-type variable. Normally, if you call toString() on your class while it is pretending to be an Object, the version of toString which will actually fire is the one defined on Object itself. However, because the function is an override, the definition of toString() from your class is used even when the class instance's true type is hidden behind polymorphism.

Overloading is the action of defining multiple methods with the same name, but with different parameters. It is unrelated to either overriding or polymorphism.

like image 41
The Digital Gabeg Avatar answered Oct 18 '22 15:10

The Digital Gabeg