- Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters.
class Dog{
public void bark(){
System.out.println("woof ");
}
public void bark(int num){
for(int i=0; i<num; i++)
System.out.println("woof ");
}
}
- On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class. Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.
class Dog{
public void bark(){
System.out.println("woof ");
}
}
class Hound extends Dog{
public void sniff(){
System.out.println("sniff ");
}
public void bark(){
System.out.println("bowl");
}
}
public class OverridingTest{
public static void main(String [] args){
Dog dog = new Hound();
dog.bark();
}
}