Overriding methods
Java allows us to provide custom implementations for inherited methods. To demonstrate this, you will use a method created in a base class and provide custom implementations of the method in the sub-class. You will achieve this by harnessing a method created in the Car class and using the override feature of Java OOP.
Este ejercicio forma parte del curso
Introduction to Object-Oriented Programming in Java
Instrucciones del ejercicio
- Inside the
Teslaclass, override and implement thesteerpublicvoidmethod. - Inside the
steermethod, print out the message"tesla steer". - Call the
steermethod using themyTeslaobject instance.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
public class Main {
static abstract class Car {
void steer() {
System.out.println("steer");
}
}
static class Tesla extends Car {
// Override steer method
@____
public void ____() {
System.out.println("tesla steer");
}
}
public static void main(String[] args) {
Tesla myTesla = new Tesla();
// Call steer method
myTesla.____();
}
}