Overloading methods
A central aspect of Polymorphism is overloading, which allows object instances created from your classes to exist in different forms. You will implement a second way to drive to enable the Tesla to drive backward. This is a case of method overloading, where we want a functionality to possess different forms within the same class.
Este exercício faz parte do curso
Introduction to Object-Oriented Programming in Java
Instruções do exercício
- Create a second
publicvoidmethod,drivethat takes abooleanparameterisDrivingBackward. - Inside the new
drivemethod, print the message"driving backward". - Call the second
drivemethod using themyTeslaobject instance with the parameter valuetrue.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
public class Main {
static class Tesla {
public void drive() {
System.out.println("drive normally");
}
// Create second drive method with parameter isDrivingBackward
public void ____(____ ____){
System.out.println("____");
}
}
public static void main(String[] args) {
Tesla myTesla = new Tesla();
myTesla.drive();
// Call second drive method
____.____(____);
}
}