Overloading and Overriding “Overloading” and “Overriding” are two important concepts that are part of the Java SE Programmer I exam. This concept, along with other core Java concepts, is important for both a developer’s perspective and for the certification exam. Overloading will be discussed first, then we will move on to 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. It is important to remember that a method can only be considered overloaded if it has a different argument set. It is not necessary to specify a different access modifier or return type. Take the following example: In the above example we are overloading the method “area” to find the area for different figures (rectangles, squares, circles). The argument lists of all three methods are different. Because they all have different argument lists, they are all overloaded methods. They also have different return value. Overloading is the simpler concept. These are the key takeaways from the exam. Any number of overloaded method can exist in the same sub-class or class. However, the argument lists of the overloaded methods must be different. Overloaded methods can be considered different methods. They have the same name, but different argument lists, return types, and access modifiers.
Overriding: We will next discuss the concept “overriding”. Sub-classes can use overriding to modify the functionality of a particular method. The concept of “overriding” is more strict than that of “overloading”. Overriding methods cannot have different return types or arguments lists. Their access modifier cannot be more restrictive and they can’t throw any new checked exceptions. This method specifies car travel.This method specifies different forms of travel. Here, the ‘public void trip()’ method in the ‘override’ class is the preferred method. When the ‘travel()” method is called on both the super-class and the sub-class, the appropriate output is generated. Let’s conclude with a few words about overriding. If the argument list in a method that we are trying override changes, it can unintentionally become an ‘overloaded’ method. Methods that aren’t inherited can’t be overridden. Methods that are marked final and static cannot be overridden. These are the same crucial points that were mentioned earlier (argument lists not changing, return types not changing and access modifiers not being more restrictive). We also discussed overloading as well as overriding in this post. In the next post, we will explore some other fascinating features of Java programming language that are related to certifications.