Overriding and Overloading: What’s the Difference?

Share if the site was helpful

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!

 

Learn How To Develop Android Apps

Share if the site was helpful

Coding is like printing your own money!!! 

Everyone has that one app idea that’s sure to be the next big thing, yet so many of these possibilities slip away unnoticed by the world.  The problem isn’t coming up with the idea, it’s crafting it into a tangible app people can download and share.   If you’ve been itching to turn an idea into reality and show the world what it’s been missing, then you’re in luck.

Phonlab’s newest course has officially been released, and to say we’re excited is an understatement.  The Android App Development course is designed to guide a student all the way from writing their first line of code ever to publishing apps on the Google Play Store.  We’ve built it to cover all the basics of Android while throwing in some snazzy features to make your creations more exciting for users.  By the end of the course you’ll not only be set up with the resources need to make a million-dollar app, but you’ll already have a portfolio of 5 apps to show off to family, friends, and potential employers.  

Here’s a quick rundown of the apps you’ll be building:

  1.  ScoreKeeper:  After learning about the basics of Java and XML this multi-sport score keeping app will show students how to build complex layouts.  Here we’ll make the jump to multiscreen apps with complex interactive layouts.  
  2.  SoundScape:  For app #2 students will learn how to include audio into their apps by building a soundboard with their own personal touches.  Audio’s not always a necessity in apps, but learning to include it can create a more immersive experience and really make your app pop.
  3.  NewsNet: In this app students will learn about this beautiful thing called the web and begin streaming live data into their apps.  We’ll consolidate articles from over 5,000 news sources into just one screen and learn some tricks the pros use to speed up development.
  4.  MyReads: Have you ever wanted to keep a log of every book you’ve ever read?  Well if so (and if not) app #4 of the course is centered around building a database for this very purpose.  We’ll learn about SQL, one of the most important languages in the data world, and learn how to share info between apps.
  5.  FireChat: In our 5th app students will build a group messaging app complete with social media incorporation by learning the syntax behind Android’s newest language Kotlin.  Kotlin has a bright future ahead of it, but online learning resources are currently few and far (Phonlab to the rescue!)

These apps are the bedrock of the education, but of course we want to set you up for success, so there are lots of other topics covered along the way.  These include how to use Github to build projects with a team of developers, and diving into the depths of object oriented programming.

Whether you want to learn as a (lucrative) career choice, or as a side hobby, there’s no better time to begin your journey, and we want to be by your side all the way.

Enroll Now At Phonlab

Phonlab’s new Apple iOS app developer course

Share if the site was helpful

Phonlab’s New Apple iOS App Developer course is finally here!!!!

We’d like to give you a warm welcome to our new iOS Application Development course at Phonlab. A course we’ve put together so that you can learn the art of iOS programming from scratch, with no hassle and no programming skills required.

In this course, you’ll learn the very basics of iOS app development in the Swift programming language. Swift was created by Apple to make iOS app development as easy as it can get. Combined with Xcode, it provides an easy route for beginners to learn how to code. Swift can also be used to build macOS, watchOS and tvOS applications, so learning it will help you a lot in the future when you’ll want to extend your knowledge 🙂

The course we provide is different than any other course. Why? Well, aside from the fact that we provide you the basics you need to have in order to be able to make iOS apps, we also provide you good real-world examples, DOs, DON’Ts, and support when you get stuck. You can ask us to review your code, and we assist you in putting your first app into the app store. We focus a lot on the basics as nothing compares with a good foundation when it comes to programming.

In this course, you will learn about Variables, Constants, Loops, Functions, Classes, Inheritance in Swift, and so much more. You’ll learn about Frameworks, CocoaPods and code examples and where to find documentation for each of these. You’re going to also gather some basic photo editing and graphics creation skills that will help you build your User Interface so that your users will love your app.

Security

We can’t leave behind the aspect of information security – not in a world where Jailbreaks, roots, hacks, and mods exist all over the place. Your application can be safer if you understand the basics of iOS security, that’s why we provide you a brief introduction to the world of Jailbreaking (modifying iOS), and we help you understand the risks your users take when they use Jailbreak methods so that you can make sure sensitive data never escapes from your app.

Publishing your first application to the iOS App Store is a little bit of a cause for celebration but at the same time it can be quite challenging, that’s why you get our assistance, and we’ll help you achieve your goals as a Apple iOS Developer.

I am glad I can be your instructor on this amazing journey. By the end of this course, you’re going to know things you never knew, have skills you’ve never had and most importantly, you’ll be able to get your feet on the ground of quite possibly the greatest programming community.  By the end of this course, you’ll have enough iOS knowledge to be able to sketch your own apps, and you’ll start a long and amazing learning process on which, this time, you’ll be your own teacher.

I know you have questions. You’re wondering “will I ever learn how to make these things appear on my screen?”. You will because if you’re reading this, you’re already on the good path. It’s normal to be concerned about the difficulty of programming. Nobody said it is easy, that’s why we’re here, to teach you what we did not have the chance to learn the easy way. To make your learning process easy, fast and consistent. 

ENROLL AT PHONLAB NOW

Welcome to the real iOS, let’s learn and grow together.

George AKA GeoSn0w

NOTE: Sign up for one year and save big with COUPON CODE GEOSN0W at check out.

Yes the 0 is a number in SN0W

Phonlab E-Campus Free Course

Share if the site was helpful

Phonlab E-Campus free Chromebook Support course

I have been working with Phonlab for many months now and we decided to bring everyone a FREE course on Chromebooks to help techs and repair shops trouble shoot and fix customer devices. If you want to get access to this FREE course just head over to Phonlab.Teachable.com and enroll. Once you enroll in the Chromebook Support course you will have access to all the lessons. We hope you enjoy them and if you find them handy you may want to check out Phonlab E-Campus where we cover smartphone repairs and security.  Phonlab has just added our own tool called MotoReaper and it can remove FRP factory reset protection lock on any Motorola device on the market today. It is an amazing tool and all students at Phonlab E-campus get access to this tool. We hope to see you there so join us and be the future of mobile today.

 

RootJunky