Write a program with a mother class animal. Inside it define a name and an age variables, and
set_value() method.Then create two instance variables Zebra and Dolphin which write a message
telling the age, the name and giving some extra information (e.g. place of origin).
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package animal;
public class Animal {
protected String name;
protected int age;
public void setAnimals(String n, int a){
name = n;
age = a;
}
public static void main(String[] args) {
Zebra z = new Zebra();
Dolphin dolp = new Dolphin();
z.setAnimals("Ana", 5);
dolp.setAnimals("Jin", 2);
z.message_zebra();
dolp.message_dolphin();
}
}
class Zebra extends Animal{
public void message_zebra()
{
System.out.printf("The zebra named %s is %d years old. The zebra comes from Africa. \n", name, age) ;
}
}
class Dolphin extends Animal{
public void message_dolphin()
{
System.out.printf("The dolphin named %s is %d years old. The dolphincomes from Africa. \n", name, age) ;
}
}
Comments
Leave a comment