This article will elaborate on the usage and implementation of method overriding in Java.
What is “Method Overriding” in Java?
If the child class contains a similar method as specified in its parent class, it is referred to as “method overriding” in Java. By doing so, the functionalities of the child class, i.e., the overridden method, come into effect.
Considerations for “Method Overriding” in Java
- The method must contain the identical name as in its parent class.
- The method should have an identical parameter as in its parent class.
- The method allocated as “final” cannot be overridden.
Example 1: Overriding a Method in Java
In this example, the methodology of overriding a method, i.e., “function” can be carried out via inheritance:
In the above demonstration, apply the following steps:
- Firstly, create a parent(super) class named “Parent”.
- Within this class, define a function named “showOut()” and display the given message in its(function) definition.
- After that, create a child(sub) class named “Child” inheriting the “Parent” class via the “extends” keyword.
- In this class, override the function defined within its parent class by defining it with the identical name “showOut()” and display the stated message.
- In main, create an object of the child class via the “new” keyword and the “Child()” constructor.
- Lastly, invoke the function “showOut()” by referring to the created object. This will invoke the overridden function instead of the default one in the parent class.
Output
In the above output, it can be analyzed that the latter function, i.e., overridden comes into effect.
Example 2: Utilization of “super” Keyword in Method Overriding Using Java
In this particular example, the “super” keyword can be associated in order to invoke the functionalities of both the default, i.e., parent and the overridden method:
public void showOut() {
System.out.println("This is Linuxhint!");
}}
class Child extends Parent {
public void showOut() {
super.showOut();
System.out.println("This is Java!");
}}
public class Example {
public static void main(String[] args) {
Child obj = new Child();
obj.showOut();
}}
In the above code block:
- Likewise, create a parent class named “Parent” and accumulate the function “showOut()” in it, displaying the provided message.
- In the next step, create a child class named “Child” inheriting the parent class.
- Within this class, similarly, override the identical function. Also, associate the “super” keyword with the function to invoke the default, i.e., the parent class function’s functionalities.
- Lastly, create an object of the child class and invoke the overridden function.
- Algorithm: The execution will be done in such a manner that when the overridden function is invoked, and the “super” keyword will refer to the parent class function. This will result in logging the functionalities of both the parent and child classes simultaneously on the console.
Output
The above output signifies that the effect of the overridden function can be canceled as well via the “super” keyword.
Example 3: Overriding the “final” Method in Java
In this example, a function can be allocated as “final” in the parent class and later on analyzed by accessing it in its child class:
According to the above-provided code, apply the below-given steps:
- Create the parent class “Parent”.
- Within this class, allocate the function “showOut()” as “final”, as indicated by the preceding keyword.
- Now, inherit the child class “Child” from the former discussed class. Here, override the allocated “final” function in the parent class.
- In main, create an object of the “Child” class and invoke the overridden function.
Output
The execution of the above code will log the displayed error since the “final” method cannot be overridden.
Conclusion
If the child class contains a similar method as specified in its parent class, it corresponds to overriding a method in Java. This approach overrides the former methods and implements the latter. The overriding can be catered via the “super” or the “final” keywords in different ways. This blog discussed the concept of method overriding in Java.