In This Article, You Will Learn About Method Overriding.
Method Overriding – Before moving ahead, let’s learn a bit about Method Overloading in Java.
Table of Contents
Difference between Method Overloading and Method Overriding
Method Overloading | Method Overriding |
Method Overloading is defined as two or more methods having the same name with different method signatures. In other words, multiple methods with the same name and different parameters are known as Method Overloading. | Method Overriding is the implementation of methods defined in the subclass when the subclass has inherited the methods from the parent class. In other words, Method overriding calls the same methods inherited by subclasses from the parent class. |
Methods implementation takes place within a class. | Methods implementation takes place within a parent and multi subclasses. In other words, there should be a relationship of inheritance between the parent & inheritance. |
All methods are defined the same except for the number of parameters. | All methods are defined the same within subclasses. In other words, all methods from subclasses are derived from the parent class. |
Method Overloading is a type of compile-time Polymorphism. | Method Overriding is the type of run time Polymorphism. |
Complexity – Easy to understand | Complexity – Difficult to understand |
Note: Method Return Type (e.g., void) is not part of Method Overloading.
Rules for Java Method Overriding
- The method must have the same name as in the parent class.
- The method must have the same parameter as in the parent class.
- There must be a relationship (inheritance) between the parent & subclass.
Method Overloading
Steps to code method overloading
- Define a class, e.g., MethodOverloding.
- Write two or more methods with the same name and different parameters.
- Define the primary method.
- Create a class object by using the new keyword
- Call the methods.
Note All steps from 2 to 5; will come under the class defined as Method Overloading.
Example: Define a Method of Overloading.
public class MethodOverloading{
// 1st method
public void add(int a, int b){
System.out.println("Sum: " + (a+b));
}
// 2nd method
public void add(int a, float b, int c){
System.out.println("Sum: " + (a+b+c));
}
public static void main(String[] args){
MethodOverloading object = new MethodOverloading();
object.add(10, 20);
object.add(10, 20.0f, 30);
}
}
Explanation – Above all methods are defined as add(), whereas in the 1st method, two parameters, and the 2nd method, are contained in a class MethodOverloading.
Example: Define a Method of Overloading.
public class MethodOverloading{
// 1st method
public void add(int a, int b){
System.out.println("Sum: " + (a+b));
}
// 2nd method
public void add(int a, float b, int c){
System.out.println("Sum: " + (a+b+c));
}
// 3rd method
public void add(int a, int b, float c, int d){
System.out.println("Sum: " + (a+b+c+d));
}
public static void main(String[] args){
MethodOverloading object = new MethodOverloading();
object.add(10, 20);
object.add(10, 20.0f, 30);
object.add(10, 20, 30.0f, 40);
}
}
Explanation – Above all methods are defined as add(), whereas in the 1st method, two parameters; in the 2nd method, three parameters; and in the 3rd method, four parameters are contained in a class Method Overloading.
Later, a class object was created to call methods and assign parameter values.
Method Overriding
Steps to code Method Overriding
- Define a class as a parent class.
- Define the method inside the parent class.
- Define another class as a subclass of the parent class.
- Same method as in step 2.
- Define the subclass of step 3.
- Same method as in step 2.
If we need to define more subclass and methods,
- Continue step number 5 and 6.
else,
2. Define a class.
3. Define the main method.
4. Create a class object by using the new keyword
5. Call the methods.
Let’s see the problem without implementing Method Overriding and know what problem we can encounter.
Example: Define a Method Overriding.
class Overriding{
public void method1() {
System.out.println("Hello I am in parent-class.");
}
}
class Method extends Overriding{
Method object = new Method();
object.method1();
}
The problem is that I have to provide a specific implementation of the run() method in a subclass, which is why we use method overriding.
Example: Define a Method Overriding.
class Overriding{
public void method1() {
System.out.println("Hello I am in parent-class.");
}
}
class Method extends Overriding{
public void method1() {
System.out.println("Hello I am in sub-class.");
}
public static void main(String[] args){
Method object = new Method();
object.method1();
}
}
Example: Define a Method Overriding.
class Overriding{
public void method1() {
System.out.println("Hello I am in parent-class.");
}
}
class Method extends Overriding{
public void method1(){
System.out.println("Hello I am in subclass.");
}
}
class subMethod extends Method{
public void method1(){
System.out.println("Hello I am in subMethod-class.");
}
}
public class demo{
public static void main(String[] args) {
Method obj = new Method(); //sub-class
obj.method1();
}
}
Please comment below if you need help with something in the above-discussed topic and have further questions.
Connect on: