Source Code :
public class Polymorphism
{
public static void main(String [] args)
{
String nama [] =
{
"Hewan",
"Anjing",
"Bebek",
"Kucing"
};
Hewan [] hewan =
{
new Hewan(),
new Anjing(),
new Bebek(),
new Kucing()
};
for(int i = 0; i < hewan.length && i < nama.length; i++)
{
System.out.println("Suara " + nama[i] + " \t: " + hewan[i].suara());
}
}
}
class Hewan
{
public String suara()
{
return "Unknown";
}
}
class Anjing extends Hewan
{
public String suara()
{
return "Guk.. Guk..!!";
}
}
class Bebek extends Hewan
{
public String suara()
{
return "Wek.. Wek..!!";
}
}
class Kucing extends Hewan
{
public String suara()
{
return "Miaow...!!";
}
}
Outputnya :
0 comments:
:)) :)] ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} ~x( :-t b-( :-L x( =))
Post a Comment