Get startedGet started for free

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.

This exercise is part of the course

Introduction to Object-Oriented Programming in Java

View Course

Exercise instructions

  • Inside the Tesla class, override and implement the steer public void method.
  • Inside the steer method, print out the message "tesla steer".
  • Call the steer method using the myTesla object instance.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class Main {
    
    static abstract class Car {
        void steer() {
            System.out.println("steer");
        }
    }
    
    static class Tesla extends Car {
        
        // Override steer method
        ____
        ____ ____ ____() {
            ____.____.____(____);
        }
        
    }

    public static void main(String[] args) {
        Tesla myTesla = new Tesla();
        // Call steer method
        ____.____;
    }
}
Edit and Run Code