Method Overloading and Method Overriding in Java
Method Overloading :
- Method Overloading is a Compile time(static binding) polymorphism.
- In method overloading, more than one method shares the same method name with a different signature in the class. In method overloading the return type can be the same or different.
- No relationship required between the overloaded methods.
- Overloaded methods can have different access modifiers.
- Overloading allows methods with different functionality to have the same name.
- Method overloading is achieved within a single class by changing the method signature.
- Method Overloading occurs within a single class.
- Method overloading is achieved within a single class by changing the method signature.
- In Method Overloading method parameters can vary in type, order, or number.
Program : Method Overloading in Java
public class ProgMethodOverloading {
static int multiply(int a, int b)
{
return a * b;
}
static int multiply(int a, int b, int c)
{
return a * b * c;
}
public static void main(String args[])
{
System.out.println("multiply() method with 2 parameters : ");
System.out.println(multiply(5, 10));
System.out.println("\nmultiply() method with 3 parameters : ");
System.out.println(multiply(5, 10, 15));
}
}
Output :
multiply() method with 2 parameters :
50
multiply() method with 3 parameters :
750
Method Overriding :
- Method Overriding is a type of runtime(dynamic binding) polymorphism.
- In method overriding, a method in a child class has the same name, return type or subtype, and parameters as a method in its parent class.
- The child class provides a specific implementation for the method that is already defined in the parent class.
- Method Overriding requires an inheritance relationship between the parent and child classes.
- Overriding methods have the same or higher access modifier in the child class.
- Overriding allows child classes to provide specific implementations for inherited methods.
- Method overriding is achieved by using the @Override annotation in the child class.
- In Method Overriding method parameters have the same type, order, and number in both parent and child classes
Program : Method Overriding in Java
public class ProgMethodOverriding {
public static void main(String args[]) {
Dog objDog = new Dog();
Animal objAnimal = new Animal();
objDog.sound();
objAnimal.sound();
// Below portion is extra guide of Polymorphism where animal reference point to Dog object
Animal objAni = new Dog();
objAni.sound();
((Dog) objAni).soundAsAnimal();
}
}
class Animal {
void sound() {
System.out.println("sound() method of Parent class");
System.out.println("Animal is speaking.");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("sound() method of Child class");
System.out.println("Dog is speaking.");
}
void soundAsAnimal() {
super.sound();
}
}
Output :
sound() method of Child class
Dog is speaking.
sound() method of Parent class
Animal is speaking.
sound() method of Child class
Dog is speaking.
sound() method of Parent class
Animal is speaking.