Polimorfisme di OOP
poly means ( many )
morphism mean ( Form )
polymorphism ( many forms )
example of polymorphism :::
1. water has many forms ( solid , liquid , gas )
2. shapes has many forms ( circle , rectangle , square )
3. sound has many forms ( loin , male , female )
types :::
1. compile time polymorphism ( static polymorphism ) ( handle by compiler )
2. run time polymorphism ( dyanmic polymorphism ) ( handle by jvm )
achieve by :::
1. compile time polymorphism ( method overloading )
2. run time polymorphism ( method overridding )
cases :::
method overloading :::
"1. if we pass character it will call int due to ( automatic promotion )
2. if we pass character it will call object if it is in the arguments
3. if number of arguments (same datatype like in promotion) is large than no automatic promotion
4. main method can be overload
5. changing the return type cannot overload"
method overridding :::
ok ::
"1. return type (covarent return type) change say ho sakta ha
(we can provide parent in parent return type and child ma return type parent ki child honi chiya)
2. access modifier
(child overridding access modifier is greater than parent overridding method)
3. overridding and exception handling
(if parent class overridding method not throw exception child class only throw check exception)
(if parent class overridding method throw exception child class only throw same as parent,samelevel,chlid exception)
4. to inherted abstract class its methods also to be inherted
5. implement ka sab methods ko run karna paray ga
6. synchronized/strictfp can also run "
donot ::
"1. final methods
2. static method
3. private method"
automatic promotion :::
"byte - short - int
char - int
int - float , double , long
float - double
long - float , double
int - float - boolean"
object :::
"object is the parent class of all the classes
( the compile perfer child class to call it first )"
varargs :::
"used for multiple arguments
( it has least property if no method match than it will run )"
difference method overloading and overridding :::
method overloading :::
1. same name
2. same class
3. different arguments ( no.of arg , type of arg , seq of arg )
method overridding :::
1. same name
2. different class
3. same arguments ( no.of arg , type of arg , seq of arg )
4. is-a relationship ( inheritance )
achieve by programing :::
method overloading :::
class test {
void show (){
sout("1")
}
void show(int a){
sout("2")
}
psvm(String[] args){
test test = new test();
test.show();
}
}
method overridding :::
class test {
void show(){
sout("1")
}
}
class xyz extends test {
void show(){
sout("2")
}
psvm(String[] args){
test test = new test();
test.show() -- it will call its own method
xyz xyz = new xyz();
xyz.show() -- it will call its own method
}
}
invoking the overridding method from subclass :::
by using "super" keywoard we can run the parent overridding method
class test {
void show(){
sout("1")
}
}
class xyz extends test {
void show(){
super.show()
sout("2")
}
psvm(String[] args){
test test = new test();
test.show() -- it will call its own method
xyz xyz = new xyz();
xyz.show() -- it will call its own method
}
}
android developer