Hi,
Difference between overloading and overriding in java is a repeatedly asked and very important interview question. So, you can find the details over here.
- Method overloading in JAVA is used to increase readability of program while method overriding is used to provide specific implementation of a method already defined in super class.
- Overloading is done within a class but method overriding is used within two classes in which inheritance is implemented.
- Method overloading is an example of compile time polymorphism while overriding is an example of run time polymorphism.
- Parameter differs in overloading while parameter's and its type always remains same in overriding.
Overloading Example
class A {
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
class A {
void myFunction(){
System.out.println("parent function...");
}
}
class B extends A {
void myFunction() {
System.out.println("child function...");
}
}
0 Comment(s)