Overloading and Overriding In Java
Method Overloading :
Methods have business logic that manipulates data(instances variable). Methods would have method name, input arguments, return type and method body. Sometimes we require to write two methods which are logically same but differ by input argument list. Let us take class “Calculator”.
public int add(int a, int b){ return a+ b; }
public int add(int a, int b, int c) { return a+b+c;}
public float add(float a, float b) { return a + b;}
The above methods have same name “add” and the expected behavior of the method is addition of input arguments. This is called as “Method Overloading”. Two or more methods of same kind of behavior overloaded with same name. Method overloading is compliment feature in Java and it gives facility to have same name. It increase readability and easy to understand the method.
Methods should be overloaded with below concerns,
1).To overload a method in java, change the method signature. Method signature is made of number of arguments, type of arguments and order of arguments if they are of different types. we have to change the combinations of them to overload a method in Java. A class can have multiple overloaded methods.
2).Return type of method is not part of method signature, so just changing the return type will not overload a method in Java.
3). Constructor can be overloaded as same as rule of method overloading.
4). Inheritance(parent-child) relationship of the classes is nothing related to method overloading. Overloading is scope of single class implementation. Method overloading is class level implementation and design.
4). Restrict the design to have maximum 4 methods with same name(method overloading). Having too many methods with same name and different parameter is hard to use. In case you require more than 4 method with same business logic, prefix/suffix the method name with proper keyword like below,
public int addInt(int a, int b){ return a+ b; } // This is not a method overloading. But improves the readability.
public float addFloat(float a, float b) { return a + b;}
5). Related to design aspect, all the method with same name should do same business logic. It should not do different logic in each overloaded method. It would make inconsistent behaviour and hard to understand the code .
public int add(int a, int b){ return a + b; //Addition of two numbers
public int add(int a, int b, int c) { return a * b * c;} // Multiple of two number with method name “add”. Never do this kind of method overloading.
public float add(float a, float b) { return a / b;)} // Division of two numbers with method name “add”. Never do this in name of method overloading.
The overloaded method would be invoked based on the input arguments and it is all decided in compile time itself.
add(11,12) => would invoke add(int, int)
add(11,12,13) => would invoke add(int, int, int)
add(11.1f, 12,2f) => would invoke add(float, float)
Here we can see that same method name “add” gives multiple behavior based on input arguments. In OOPS, the ability to process various different arguments through a single uniform interface or by single method name, it is called as “polymorphism’. Method overloading decides the method to be called in compile time. Method overloading is kind of compile time polymorphism.
Method overriding in Java:
In OOPS inheritance, we have parent class and child classes. Parent class would have data and methods. Child class may have data other than data derived from parent class. In such scenario , rewriting the parent class method in child class may require to update the behavior respect to child class data. Method overriding is applicable only in inheritance relationship. Method overriding is redefine (override) the class method in child class to add logic related to child class. For instance,
class Building{
int width, height,numberOfFloors;
//….Constructors, Getter and setter methods code….
public void display() {
System.out.println(“Height:” + height + “,Width:” + width+”,Number of Floors:”+ numberOfFloors);
}
}
class CommericalBuilding extends Building
{
int numberOfShops;
//….Constructors, Getter and setter methods code….
@Override
public void display() { //Method overriding.
System.out.println(“Height:” + height + “,Width:” + width+”,Number of Floors:”+ numberOfFloors + “, number of shops:”+ numberOfShops);
}
}
In the above example, child class has instance variable “numberOfShops”. The parent class display() method prints only parent class data information. So redefine(override) the display() method is necessary to print the numberOfShops in display() method. It is not only related to display() method. In the OOPS, there are many places we require to override the method in child classes.
Rules for overriding:
1). Overriding method should have same signature as in parent class, otherwise it would be consider as method overloading.
2). Overriding method access modifier should be same or could increase the visibility of the method. Such as protected method could be implemented as “public” in child class.
3). private, static and final method can not be overridden in Java.
4). Return type of overriding method must be same as overridden method
5). Finally overloading and overriding are totally different principles. Do not confuse by “over” word in both of the principles.
class Building{
int width, height,numberOfFloors;
//….Constructors, Getter and setter methods code….
public void display() {
System.out.println(“Height:” + height + “,Width:” + width+”,Number of Floors:”+ numberOfFloors);
}
}
class CommericalBuilding extends Building{
int numberOfShops;
//….Constructors, Getter and setter methods code….
// It is not Method overriding. it is overloading. Because display() is derived from parent class and display(string) in child class.
// Two implementation of display() method with different signature. It is method overloading.
public void display(String loggerValue) {
System.out.println(loggerValue + “,Height:” + height + “,Width:” + width+”,Number of Floors:”+ numberOfFloors + “, number of shops:”+ numberOfShops);
}
}
I believe now we are clear about method overloading vs method overriding in java. Next topic would discuss on polymorphism and how to achieve polymorphism by overriding. Happy learning!!