Method Overloading
- Method overloading means providing two or more separate methods in a class with the same name but different parameters.
- Method return type may or may not be different and that allows us to reuse the same method name.
- Method overloading is very handy, it reduces duplicated code and we do not have to remember multiple method names.
- Method overloading does not have anything to do with polymorphism but java developers often refer to overloading as Compile Time Polymorphism.
- In other words, the compiler decided which method is going to be called based on the method name, return type, and argument list.
Method overloading happens inside a single class, but a method can also be treated as overloaded in the subclass of that class that is because a subclass inherits one version of the method from the parent class and then the subclass can have another overloaded version of the method.
Rules
- Methods must have the same method name.
- Methods must have different parameters.
- Methods may or may not have different return types.
- Methods may or may not have different access modifiers.
- Throw different checked or unchecked exceptions.
Method overriding
- Method overriding means defining a method in a child class that already exists in the parent class with same signature (same name, same arguments)
- By extending the parent class the child class gets all the methods defined in the parent class (those methods are also known as derived methods).
- Method overriding is also known as Runtime Polymorphism and Dynamic Method Dispatch because the method that is going to be called is decided at runtime by the JVM.
- When we Method override a method it’s recommended to put
@Override
immediately above the method definition. This is an annotation that the compiler reads and will then show us an error if we do not follow overriding rules correctly. - We can not override static methods only instance methods.
Rules
- It must have the same name and same arguments
- Return type can be a subclass of the return type in the parent class.
- It can not have a lower access modifier.
- For example, if the parent method is protected then using private in the child is not allowed but using public in the child would be allowed.
- Only inherited methods can be overridden, in other words, methods can be overridden only in child classes.
- Constructors and private methods cannot be overridden.
- Methods that are final cannot be overridden
- A subclass can use super.methodName() to call the superclass version of an overridden method.
Method overloading
package anote;
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.engineSound();
car.engineSound(5);
}
}
// Method overloading in the same class
class Car {
public void engineSound() {
System.out.println("www");
}
public void engineSound(int num) {
String sound = "";
for (int i = 0; i < num; i++) {
sound = sound + "w";
}
System.out.println("www" + sound);
}
}
java anote/Main.java
www
wwwwwwww
Method overriding
package anote;
public class Main {
public static void main(String[] args) {
Car car = new Car();
Lamborghini lamborghini = new Lamborghini();
car.engineSound();
lamborghini.engineSound();
}
}
// Override with a inherited method
class Car {
public void engineSound() {
System.out.println("www");
}
}
class Lamborghini extends Car {
@Override
public void engineSound() {
System.out.println("boooooaaaa!!!");
}
}
java anote/Main.java
www
boooooaaaa!!!