Polymorphism means "many forms," and in Java, it refers to methods and classes that have many forms. This is a slightly confusing topic when you're first getting started. So don't worry if it takes a minute to fully wrap your head around it.
How to Use Polymorphism in Java?
You have already been using polymorphism without really knowing about the word, so now it's time to dive into it! There are three ways to implement polymorphism:
1. Overloading Methods
When you overload methods, you define several different functionalities behind what appears to be a single method call. The System.out.println() method is a perfect example of polymorphism via overloading. The println() method is defined many times over, with each definition accepting different arguments such as String, int, Objects, and so forth. Because of this, you can call System.out.println() and pass it to just about any data type, and it will work.
This is interesting because Java is strictly typed. Meaning each method must define the data types of its method parameters. So why can you call System.out.println() and pass it to just about any data type? It is because it is overloaded many times. This gives the appearance and functionality of a single method acting in a variety of ways, or, in this case, accepting a variety of different data types as arguments. This is an example of "many forms" - polymorphism.
2. Overriding the Behavior of Parents Methods
When you override one or more parent methods, as you have seen in previous sections, you are adding multiple functionalities behind a single method call. Such as the start() method you saw in the Vehicle and Motorcycle classes. Since you have a start() method in each, and you can call either one depending on the context, you have an example of polymorphism: the start() methods act and/or appear as a single, unique method call but in fact, there are two methods - each with its own functionality.
3. Implementing Interfaces
Interfaces are the primary driver of polymorphism in Java. Interfaces define methods that all of their implementing classes must define. Each class that implements the interface will define its own unique functionality for those methods. Since each class that implements an interface is now considered a "type" of that interface, you can then use that interface type as an instance variable (dependency) in other classes - much like you saw with the MotorizedVehicle instance variable in the previous section. This allows classes to be very flexible and to act in a variety of different ways -> "polymorphism".
4. Implementing Abstract Classes
Implementing an abstract class is a form of polymorphism known as "subtype polymorphism" or "inheritance polymorphism." Polymorphism, in general, refers to the ability of a single interface or method to represent different types or behaviors. Subtype polymorphism allows objects of a derived class (subclass) to be treated as objects of their base class (superclass).
Summary: What is Polymorphism in Java?
- Polymorphism means many forms
- Polymorphism refers to methods and classes having multiple forms
- There are four main implementations of polymorphism in Java
- A very good example of polymorphism is the
System.out.println()method. You can pass any value of any type to this "one" method and it works. How? Because this one method is actually overloaded many times over. Thus, what appears to be a single method that has many forms.
Implementation of Polymorphism in Java
- Overloading methods
- Overriding the behavior of parent methods
- Implementing interfaces
- Implementing Abstract Classes