Implementing interface on Tesla class
To make the Tesla class have the self-driving feature, you will use the ElectricCar interface to enable this feature. In the Tesla class, you will harness the interface and implement the self-driving method as a requirement.
NOTE: Interface non-void methods must be implemented in the class implementing the interface.
This exercise is part of the course
Introduction to Object-Oriented Programming in Java
Exercise instructions
- Implement the
ElectricCarinterface using theimplementskeyword on theTeslaclass. - Implement the
voidactivateSelfDrivingmethod inside theTeslaas apublicmethod, and make it print out"self driving on"when called. - Call the
activateSelfDrivingmethod on themyTeslaobject instance.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
public class Main {
interface ElectricCar {
void activateSelfDriving();
}
// Implement ElectricCar Interface
static class Tesla ____ ____ {
// Implement activateSelfDriving method
public void ____() {
System.out.____("self driving on");
}
}
public static void main(String[] args) {
Tesla myTesla = new Tesla();
// Call activateSelfDriving method
____.____();
}
}