This code for the beginner who really want to understand about the @Override
process, this will help you! (Remind inheritance concept of Java.)
For example, the Fish
class might have two subclasses: FreshwaterFish
and SaltwaterFish
.
These subclasses would have all the features of the Fish
class, but could further customize the objects through new attributes and behaviors or modified behaviors from the parent class Fish
. For example, the FreshwaterFish
class might include information about the type of freshwater environment lived in (e.g. river, lake, pond, or puddle).
Similarly, the SaltwaterFish
class might customize the makeBabyFish()
method such that the fish eats its mate after breeding (as defined in the super class) by using the override mechanism, like this:
public class SaltwaterFish extends Fish
{
@Override
public void makeBabyFish(Fish fishSpouse, int numBabies) {
// call parent method
super.makeBabyFish(fishSpouse, numBabies);
// eat mate
eat(fishSpouse);
}
}