‘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:
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.
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:
- Any number of overloaded methods can exist in the same class or sub-class
- The overloaded methods MUST differ in their argument lists.
- 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:
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:
- If the argument list changes in a method we are trying to override, then it unintentionally becomes an ‘overloaded method’.
- Methods that are not inherited cannot be overridden.
- Methods which are marked static and final cannot be overridden.
- 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.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021
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..