Strategy 5
An abstract class is like an interface except you can include code. Since it is a class, the rule of “you can only extend one class” applies, so it actually has a different job than an interface even though it looks similar.
-
Make an abstract class
Q
with:- Constructor that takes in the
String
favorite food. - Abstract methods
void eat()
,void washHands()
andvoid brushTeeth()
. - Concrete method:
void lunch()
which does wash hands and eat. - Concrete method:
void dinner()
which does wash hands, eat, and brush teeth. - A
String ff()
method to get the favorite food.
- Constructor that takes in the
-
Create a class
R
extendingQ
.- Constructor takes in the favorite food.
- Wash hands prints out “hands are clean”
- Brush teeth prints out “happy teeth”
- Eat prints out “Crunch crunch crunch”
-
Create a class
RR
extendingR
.- Constructor takes in a favorite food.
- Eat is changed so that it prints out “Cheerios” the first time it is called, “Sandwich” the second time, and the favorite food every time after that.
-
Create
RRR
extendingRR
.- Favorite food is always “pasta”.
- Instead of washing hands it prints “my hands are clean enough”.
-
Tester/puzzle code:
public static void testQ(Q x) {
x.lunch();
x.dinner();
}
public static void tester() {
RR x = new RRR();
R y = new RR("Frijoles");
R z = new R("Banana cream pie");
testQ(x);
testQ(y);
testQ(z);
}