Difference Between Method Overloading and Method Overriding in Java

Method Overloading:
1) Performed within class
2) Parameters must be different
3)Is the example of compile time polymorphism
4)Return type can be the same or different in method overloading.But you must have to change the parameter.
Method Overriding:
1)Method overriding occurs in two classes that have IS-A(inheritance) relationship
2)Parameters must be the same
3) Is the example of run time polymorphism.
4) Return type must be the same or covariant in method overriding

Overloading Example:

public class OverloadingExample {
static int add(int a,int b)
{
	return a+b;
}
static int add(int a,int b,int c)
{
	return a+b+c;
}
}

 

Overriding Example:

public class OverridingExample {
class Animal{
	void eat(){
		System.out.println("Eating.....");
	}
}
class Dog extends Animal{
	void eat(){
		System.out.println("eating bread.....");
	}
}
}

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.