Overloading and Overriding

‘Overloading’ and ‘Overriding’ is yet another crucial concept which is part of the ‘Java SE Programmer I’ exam. This concept like the other concepts of core Java, are important from a developer’s standpoint as well as for the certification exam. We will first discuss overloading and then move onto overriding.

Overloading:
‘Overloading’ as the name suggests is done by creating multiple methods with same name – but with different argument lists, return type and access modifier. Out of the three points mentioned, it is important to note that a method is considered overloaded ONLY when it has a different argument list. Specifying a different return type and access modifier is optional.

Consider the example specified below:

Overloading-Ex1

 In the above example, we are overloading the method ‘area’ to find the area of different figures (rectangle, square, circle). All the three overloaded methods differ in their argument lists.

Overloading-Ex2

Since, all methods feature different argument lists they are overloaded methods. In addition, they also have different return values. Overloading is by far the simpler of two concepts and these are the key takeaways for the exam:

  1. Any number of overloaded methods can exist in the same class or sub-class
  2. The overloaded methods MUST differ in their argument lists.
  3. Overloaded methods are really different methods – they just have the same name but with different argument lists, return types and access modifiers.

 

Overriding:
We next discuss the concept of ‘overriding’. Overriding is used in sub-classes to change the functionality of a method for that particular class. The concept of ‘overriding’ is much more stringent than the concept of ‘overloading’. Overriding methods should NOT have different arguments lists or return types. Their access modifier CANNOT be more restrictive and they cannot throw any new checked exceptions.
We will illustrate this by an example:

Overriding-Ex1

 This will be the output of the above example.

This method specifies car travel
This method specifies the different forms of travel

Here, ‘public void travel()’ in the ‘overrride’ class is the overridden method.  The appropriate output is generated when the ‘travel()’ method is called on the super class as well as the sub-class.

To conclude, here are a few points regarding overriding:

  1. If the argument list changes in a method we are trying to override, then it unintentionally becomes an ‘overloaded method’.
  2. Methods that are not inherited cannot be overridden.
  3. Methods which are marked static and final cannot be overridden.
  4. In addition to this, the crucial points stated earlier hold as well(argument lists, return types not changing, access modifier not being more restrictive and so on)

We discussed overloading and overriding in this post. We will discuss some more fascinating features of the Java programming language relating to the certifications in the next post.

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

1 thought on “Overloading and Overriding”

  1. Earleen Hetherman

    I am extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it’s rare to see a great blog like this one today..

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top