Enter your email address:

    Delivered by FeedBurner

Program Polymorphisms in Java


import java.util.Random;

class Animal{

private String tipe;



public Animal(String tipe){

this.tipe = tipe;

}

public String toString(){

return "This is "+tipe;

}

public void TheVoice(){

}

}

class Dog extends Animal{

private String name;

private String type;

public Dog(String name){

super("Dog");

this.name = name;

type = "unknown";

}

public Dog(String name, String type){

super("Dog");

this.name = name;

this.type = type;

}

public void TheVoice(){

System.out.println("Guk guk guk");

}

public String toString(){

return super.toString()+"\nThis is "+type+" the name "+name;

}

}

class Chicken extends Animal{

private String name;

private String type;

public Chicken(String name){

super("Chicken");

this.name = name;

this.type = "unknown";

}

public Chicken(String name, String type){

super("Chicken");

this.name = name;

this.type = type;

}

public String toString(){

return super.toString()+"\nThis is "+type+" the name "+name;

}

public void TheVoice(){

System.out.println("Kukuruyuk");

}

}

class Cat extends Animal{

private String name;

private String type;

public Cat(String name){

super("Cat");

this.name = name;

this.type = "unknown";

}

public Cat(String name, String type){

super("Cat");

this.name = name;

this.type = type;

}

public void TheVoice(){

System.out.println("Meong meong meong");

}

public String toString(){

return super.toString()+"\nThis is "+type+" the name "+name;

}

}

class Polimorfisma{

public static void main(String args[]){

Animal[] TheAnimal = {

new Dog("Brino", "Herder"),

new Cat("Lili", "Persia"),

new Chicken("Chucky", "Kate")

};

Animal ChooseAnimal;

Random choose = new Random();

for (int i = 0; i<5>

ChooseAnimal = TheAnimal[choose.nextInt(TheAnimal.length)];

System.out.println("\nYour choose : \n" + ChooseAnimal);

ChooseAnimal.TheVoice();

}

}

}


By : Asep Herman Suyanto


import java.util.Random;

class Animal{

private String tipe;



public Animal(String tipe){

this.tipe = tipe;

}

public String toString(){

return "This is "+tipe;

}

public void TheVoice(){

}

}

class Dog extends Animal{

private String name;

private String type;

public Dog(String name){

super("Dog");

this.name = name;

type = "unknown";

}

public Dog(String name, String type){

super("Dog");

this.name = name;

this.type = type;

}

public void TheVoice(){

System.out.println("Guk guk guk");

}

public String toString(){

return super.toString()+"\nThis is "+type+" the name "+name;

}

}

class Chicken extends Animal{

private String name;

private String type;

public Chicken(String name){

super("Chicken");

this.name = name;

this.type = "unknown";

}

public Chicken(String name, String type){

super("Chicken");

this.name = name;

this.type = type;

}

public String toString(){

return super.toString()+"\nThis is "+type+" the name "+name;

}

public void TheVoice(){

System.out.println("Kukuruyuk");

}

}

class Cat extends Animal{

private String name;

private String type;

public Cat(String name){

super("Cat");

this.name = name;

this.type = "unknown";

}

public Cat(String name, String type){

super("Cat");

this.name = name;

this.type = type;

}

public void TheVoice(){

System.out.println("Meong meong meong");

}

public String toString(){

return super.toString()+"\nThis is "+type+" the name "+name;

}

}

class Polimorfisma{

public static void main(String args[]){

Animal[] TheAnimal = {

new Dog("Brino", "Herder"),

new Cat("Lili", "Persia"),

new Chicken("Chucky", "Kate")

};

Animal ChooseAnimal;

Random choose = new Random();

for (int i = 0; i<5>

ChooseAnimal = TheAnimal[choose.nextInt(TheAnimal.length)];

System.out.println("\nYour choose : \n" + ChooseAnimal);

ChooseAnimal.TheVoice();

}

}

}


By : Asep Herman Suyanto


Qidzama

Recent Articles

Blog Archive