- It is really important to provide students with tangible motivation for interfaces and inheritance.
- Activity:
- Ask a student to draw a "general car."
- They’ll quickly realize (or you’ll point out) that you’re not drawing a general car. Rather they are drawing a specific car! (a Bug, a Jeep, etc.)
- Connect this realization to the idea of interfaces.
- Make interfaces intuitive by explaining that you can never really have a general car, but there is shared behaviors (i.e. methods) for cars.
- Additionally, cars wouldn’t be an abstract class because the internal components of a car might have no similarity.
- Then, create sample code for the interface Car and an inheriting class like the following:
public interface Car{
String brand, model;
double speed;
int xCoord, int yCorod;
public void move();
}public class OffRoadCar implements Car{
boolean bikeRack=true;
public void move(){
xCoord++;
}
}