Reading
Add Comment
Overloading:
- Using same name of method with multiple implementation in within class.
- Method overloading is performed within class.
- Method overloading is used to increase the readability of the program. (if defined print method for string as well as integers than at run time it will call the respective print
- of the kind which we used).
- In case of method overloading, parameter must be different.
- Method overloading is the example of compile time polymorphism.
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:
- Method overriding is used to provide the specific implementation of the method that is already provided by its super class
- Method overriding is the example of run time polymorphism.
- Method overriding occurs in two classes that have IS-A (inheritance) relationship.
- Return type must be same or covariant in method overriding.
class Animal{
void eat(){
System.out.println("eating...");}
}
class Dog extends Animal{
void eat(){
eat(); ///parent's class eat
System.out.println("eating bread...");}
}
0 comments:
Post a Comment