What is Method Overriding in Java? Difference between Method Overriding and Method Overloading in Java – Java Tutorial
Solution:
Method overriding is a mechanism in which a subclass inherits the methods of a superclass and sometimes the subclass modifies the implementation of a method defined in the superclass.
Video Tutorial – Method Overriding in Java
The method of superclass which gets modified in a subclass has the same name and type signature.
The overridden method must be called from the subclass.
The overridden method must be called from the subclass.
Consider following Java Program, in which the method (named as fun ) in which a is assigned with some value is modified in the derived class.
When an overridden method is called from within a subclass, it will always refer to the version of that method re-defined by the subclass.
The version of the method defined by the superclass will be hidden.
Programming example to demonstrate method overriding
class A { int a=0; void fun(int i) { this.a=i; } } class B extends A { int b; void fun(int i) { int c; b=20; super.fun(i+5); System.out.println(“The value of a:“+a); System.out.println(“The value of b:"+b); c=a*b; System.out.println("The value of c: "+c); } } class OverrideDemo { public static void main(String args[]) { B obj_B=new B(); obj_B.fun(10); //function re-defined in derived class } }
Output:
The value of a: 15
The value of b: 20
The value of c: 300
Program Explanation
In the above program, there are two classes – class A and class B.
Class A acts as a superclass and class B acts as a subclass.
In class A, a method fun is defined in which the variable a is assigned with some value.
In the derived class B, we use the same function name fun in which, we make use of a super keyword to access the variable a, and then it is multiplied by b and the result of multiplication will be printed.
Another Example to demonstrate Method overring in Java
class A { void display() { System.out.println("Super class method"); } } class B extends A { void display() { super.display(); System.out.println("Derived class method"); } } public class MethodOverriding { public static void main(String args[]) { B obj = new B(); obj.display(); } }
Output:
Super class method
Derived class method
Difference between Method Overloading and Method Overriding in Java
Method Overloading | Method Overriding |
The method overloading occurs at compile time. | The method overriding occurs at the run time or execution time. |
In the case of method overloading, a different number of parameters can be passed to the function. | In function overriding the number of parameters that are passed to the function is the same. |
The overloaded functions may have different return types. | In method overriding all the methods will have the same return type. |
Method overloading is performed within a class. | Method overriding is normally performed between two classes that have an inheritance relationship. |
Summary:
In this article, we understood, What is Method Overriding in Java? Difference between Method Overriding and Method Overloading in Java – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.