Search⌘ K
AI Features

Solution Review: Displaying Message Using Inheritance

Explore how to implement inheritance in Java by creating an Animal superclass and Zebra and Dolphin subclasses. Learn to use private members with getter methods, set data through constructor-like methods, and display messages inherited from the parent class to build reusable code.

Solution: Displaying message using inheritance #

Java
class Animal {
private int age;
private String name;
void set_data(int a, String b) {
age = a;
name = b;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
class Zebra extends Animal {
String message_zebra(String str) {
str = "The zebra named " + getName() + " is " + getAge() + " years old. The zebra comes from Africa";
return str;
}
}
class Dolphin extends Animal {
String message_dolphin(String str) {
str = "The dolphin named " + getName() + " is " + getAge() + " years old. The dolphin comes from New Zealand";
return str;
}
}

Understanding the Code

...