Polymorphism: Method Overloading

Image result for meteor little shop of horrors

In a previous blog( in an early year of a decade not too long before our own…) I described the concept of polymorphism in Java, focusing on one of the types of polymorphism exclusively, method overriding. I did mention method overloading, the other type, and gave you a link to explore it on your own. If you did, great job for following up on your curiosity! If you didn’t…That’s perfectly fine too! Sometimes you don’t need to know every way to do things but you can fix that now because I feel that it’s a gap in my blog I need to fill. So let’s get started!

First, let’s do a quick review of polymorphism. Polymorphism is one of the four pillars of object-oriented programming, which means ‘many forms’. It can occur through the use of inheritance, which allows subclasses to inherit methods and attributes from other classes. Method overriding happens when a subclass provides a special signature for the method inherited from a class at run-time. Full details on method overriding can be found here.

Method overloading, on the other hand, is when different methods can have the same name but different signatures, namely type and number of input parameters. Let’s check out an example so we can compare it to the link above’s method overriding example.

First we need a class to define our methods in, I called it Feline for this example.

public class Feline {

	public void describe(String name) {
		System.out.println("I am " + name + ". I am a large 
                orange cat with black stripes!");
	}
	
	public void describe(String name, String color, String markings) {
		System.out.println("I am " + name + ". I am a small 
                " + color + " cat with " + markings + "!");
	}

}

As you can see we have two methods that send a line to the console to be displayed; one has one parameter and the other takes three parameters. The twist here, is that they’re both the same method name of describe!

Moving onward, we make our main class, which I call CrazyCatLady. Who else would have both a tiger and normal housecats after all? Here we create a new Feline object and invoke our methods.

public class CrazyCatLady {

	public static void main(String[] args) {
		Feline obj = new Feline();
		obj.describe("Rahjah");
		obj.describe("Berlioz", "dark gray", "lighter gray chest");
		
	}

}

As you can see, we pass a string into the first describe method and the three strings into the second describe method. Execute the code and this is what we get in the console:

First line is from the single-parameter signature of the describe method and the second line is from the multiple-parameter signature of the describe method.

I’m going to be honest with you, dear reader. I began writing this blog not really fully understanding this concept myself! Through an accidental writing of this code in the form of an overriding polymorphism(whoops!) and the successful writing of this one I now understand the difference between the two pretty well. The main things to remember about method overloading vs overriding is that overloading concerns the use of special method signatures that have a different number of parameters and that the choice between the two methods will occur at runtime of the program.

One thought on “Polymorphism: Method Overloading

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.