What is the use of overloading and overriding in Java?
The overloading method is used in Java when two or more methods share the same name in the same class but with different parameters.
class Dog{
public void bark(){
System.out.println("woof ");
}
//overloading method
public void bark(int num){
for(int i=0; i < num; i++) {
System.out.println("woof ");
}
}
}
The overriding method defines the case where the child's class redefines the same method as their parent class. The overridden methods should have the same argument list, name, and return type.
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();
}
}