Home > Java > Polymorphism dalam Java

Polymorphism dalam Java

Polymorphism

Setelah inheritance, hal yang penting bagi object oriented programming adalah polymorphism. Secara leksikal polymorphism merupakan sesuatu yang mempunyai banyak bentuk. Dalam pemrograman polymorphism merupakan dapat diartikan suatu modul yang mempunyai nama sama, tapi memiliki behavior yang berbeda sehingga listing code implementasinya berbeda. Dan dalam bahasa yang lain adalah kemampuan dari reference untuk mengubah sifat sesuai dengan object yang dijadiin acuan. Polymorphism menyediakan menyediakan multiobject dari sub class yang berbeda diperlakukan sebagai object tunggal dari super class.

Contoh :

Bicycle.java

public class Bicycle {
public int gear;
public int speed;
public int power;

public Bicycle(int startGear, int startSpeed, int startPower)
{
gear = startGear;
speed = startSpeed;
power = startPower;
}

public void setPower( int newValue)
{
power = newValue;
}
public void setGear( int newValue)
{
gear = newValue;
System.out.println(“Gear At”+ gear);
}
public void applyBreake(int decrement )
{
speed -= decrement;
}
public void speedUp(int increment)
{
speed += increment;
}
public void printDescription()
{
System.out.println(“Bike in gear”+this.gear+”with power”+this.power+”and speed”+this.speed);
}

}

MountainBike.java

public class MountainBike extends Bicycle {
private String suspension;

public MountainBike(int startSpeed, int startGear, int startPower, String startSuspension )
{
super(startGear, startSpeed, startPower);
setSuspension(startSuspension);
}

public String getSuspension(){
return this.suspension;
}

public void setSuspension(String suspensionType){
this.suspension = suspensionType;
}

@Override
public void printDescription(){
super.printDescription();
System.out.println(“The MountainBike has a ” + getSuspension()
+ ” suspension.”);
}

}

RoadBike.java

public class RoadBike extends Bicycle {
private int tireWidth;
public RoadBike(int startSpeed, int startPower, int startGear, int newTireWidth)
{
super(startGear, startSpeed, startPower);
setTireWidth( newTireWidth);
}
public int getTireWidth(){
return this.tireWidth;
}

public void setTireWidth(int newTireWidth){
this.tireWidth = newTireWidth;
}

@Override
public void printDescription(){
super.printDescription();
System.out.println(“The RoadBike has ” + getTireWidth()
);
}

}

Main.java

public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Bicycle bike01, bike02, bike03;

bike01 = new Bicycle(20, 10, 1);
bike02 = new MountainBike(20, 10, 5, “Dual”);
bike03 = new RoadBike(40, 20, 8, 23);
MountainBike Ren = new MountainBike(99,9,9,”Dual”);

bike01.printDescription();
bike02.printDescription();
bike03.printDescription();

}

}

Semoga Bermanfaat.^_^

  1. April 11, 2011 at 11:11

    makasi ya,, materinya sangat membantu🙂

    • Ren
      April 11, 2011 at 13:01

      sama2,kakak.:D

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: