Overriding and Overloading: What’s the Difference?
When programming for Android (or anything) writing custom methods isn’t optional. If you don’t know what it means to write a method, then I recommend you check out Phonlab’s video tutorials. And if you do, then don’t tune out yet! Methods are like icebergs (90% of their functionality is unseen at first glimpse). And getting to know the other 90% is what can take you to the next level in your development. In this post we’re going to explore the ins and outs of overriding and overloading to make development easier.
Methods 101:
If you’re an Android programmer, then you undoubtedly know what a method is. It’s a block of code that you give a name. This way every time you call it that block is executed. I know it’s a fundamental concept of programming, but here’s a sample that we’ll build onto over time. Let’s say that you’re building a contacts app and can add contacts given a name and phone number. Here’s the class your app uses:
Great, seems simple enough right? But any good contacts app is going to do more than just this. If a user wants, they should be allowed to include other info as well. What if they want to put one contact on speed dial? Or what if they want to add a contact to a group? How about this:
Now, there’s no problem with this code. Each of these three methods will add a contact with the extra information that they included. But these method names could start turning ugly if we kept adding parameters. How do you feel about the method name addNewContactWithSpeedDialAndGroupAndPictureAndAgeAndRingtone? First off, I’d say let’s drop every “And”, but even so that method name is getting long. This is where overloading can help.
Overloading:
Overloading is the practice of creating new methods with the same name. But you can’t have two identical method names, right? If they had different logic how would the computer know which one to execute? Well they actually can as long as the parameters are different. As long as your methods each differ like this:
then the compiler is able to recognize them each as individual methods. So now if a user wants to add a contact it doesn’t matter how much information they give! They can either call addNewContact(Carl, “1112223333”) or addNewContact(“Carl”, 1112223333, “Friends”) and their new contact will be added with that info.
Order counts too, you could have the methods addNewContact(String name, int phoneNumber) and addNewContact(int phoneNumber, String name) as two separate methods in the same class. This doesn’t open many doors in this specific scenario, but it’s handy to know all the same.
Overriding:
While it sounds almost identical, overriding a method is a somewhat different technique. This allows us to take a method that exists in a parent class and change its behavior. As a demonstration let’s say that our contacts app has a page where you can look at individual groups. When you open the group for “Family” there is a button where you can add contacts to this group.
Our new class named FamilyGroup will extend our NewContact class. By doing so it now has access to the parent class’ methods. But we don’t want our user to have to type in the group they are going to use. That’s a waste of time for them since they’re already in “Family”. To fix this we’ll override our addNewContact(int phoneNumber, String name) like so:
Now when a user puts in info for a new contact’s name and phone number this method will call NewContact’s addNewContact(int phoneNumber, String name, String group) and pass in “Family” for the group. Sure, we could have written another function in this class to add the contact, but this way no matter how complicated NewContact’s method is, we get to call it again with only one line of code!
Wrapping up:
Overriding and overloading can make your code a lot neater if used properly, and there’s a tone of cool things you can do with these techniques in the real world. This contacts app isn’t the most realistic project, but it gets the simple idea across. When you start getting into more complex inheritance overriding can save you a ton of space.
If you enjoyed this post at all and want to learn more about Android development check out some of our other tutorials such as how to develop augmented reality or more complex apps!
Now I have understood the concept about overriding and overloading. Thanks