Polymorphism adalah : suatu object dapat memiliki berbagai bentuk,sebagai object dari classnya sendiri atau object dari superclassnya.
Polymorphism ada 2 yaitu :
Polymorphism overloading
Polymorphism overriding
Polymorphism overloading adalah : penggunaan satu nama untuk beberapa method yang berbeda (beda paramater)
Contoh :
Public class point2D{
Int x,y;
public Point2D(){
x=0; y=0;
}
Public Point2D(intnx,int ny){
setPoint(nx,ny);
}
//setter method
Public setPoint(int nx,int ny){
x = nx;
y = ny;
}
//continue class Point declaration
//getter method
Int getX() { return x; }
//overloading method from class object
Public String toString(){
Return “x: “+x”,y: ”+y;
}
}//end of class declaration
Int getY() { return y; }
Polymorphism overriding terjadi ketika deklarasi method subclass dengan nama dan parameter yang sama dengan method dari superclassnya.
Contoh :
Public class point2D{
Int x,y;
public Point2D(){
x=0; y=0;
}
Public Point2D(intnx,int ny){
setPoint(nx,ny);
}
//setter method
Public setPoint(int nx,int ny){
x = nx;
y = ny;
}
//continue class Point declaration
//getter method
Int getX() { return x; }
//overriding method from class object
Public String toString(){
Return “x: “+x”,y: ”+y;
}
}//end of class declaration
Int getY() { return y; }
Tidak ada komentar:
Posting Komentar