After teaching inheritance and implementation (implements
vs extends
in Java) and overriding methods and basic OOP (classes and interfaces), as well as polymorphism (up/downcasting) I want to give the students some exercises to practice this.
Currently, I have the famous Animal -> Mammal
example, as well as Human -> Teacher
, Human -> Student
.
I am trying to think of new examples and exercises to practice the subjects mentioned. To give a better notion of what I'm looking for, the Animal
exercise looks like this:
public abstract class Animal implements AnimalActions {
private String name, family;
private double weight;
public Animal(String name, String family, double weight) {
this.name = name;
this.family = family;
this.weight = weight;
}
}
interface AnimalActions {
public void eat();
public void sleep();
public void makeSound();
}
And then:
public class Mammal extends Animal {
public Mammal(String name, String family, double weight) {
super(name, family, weight);
}
@Override
public void eat() {
}
@Override
public void sleep() {
}
@Override
public void makeSound() {
}
}
This covers many things and is useful for teaching. However, this doesn't go very far if I make it into a practice exercise. What exercises do others give students in this subject?
The students are in high school.
ArrayList<Animal> a = new ArrayList<Animal>(); a.add(new Mammal());
)? $\endgroup$