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.
Solution 1:
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
...
Solution 2:
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.
Solution 3:
Polymorphism means more than one form, same object performing different operations according to the requirement.
Polymorphism can be achieved by using two ways, those are
- Method overriding
- Method overloading
Method overloading means writing two or more methods in the same class by using same method name, but the passing parameters is different.
Method overriding means we use the method names in the different classes,that means parent class method is used in the child class.
In Java to achieve polymorphism a super class reference variable can hold the sub class object.
To achieve the polymorphism every developer must use the same method names in the project.
Solution 4:
Both overriding and overloading are used to achieve polymorphism.
You could have a method in a class that is overridden in one or more subclasses. The method does different things depending on which class was used to instantiate an object.
abstract class Beverage {
boolean isAcceptableTemperature();
}
class Coffee extends Beverage {
boolean isAcceptableTemperature() {
return temperature > 70;
}
}
class Wine extends Beverage {
boolean isAcceptableTemperature() {
return temperature < 10;
}
}
You could also have a method that is overloaded with two or more sets of arguments. The method does different things based on the type(s) of argument(s) passed.
class Server {
public void pour (Coffee liquid) {
new Cup().fillToTopWith(liquid);
}
public void pour (Wine liquid) {
new WineGlass().fillHalfwayWith(liquid);
}
public void pour (Lemonade liquid, boolean ice) {
Glass glass = new Glass();
if (ice) {
glass.fillToTopWith(new Ice());
}
glass.fillToTopWith(liquid);
}
}