Aan de slagGa gratis aan de slag

Implementing abstract methods

You will change the Toyota and Mercedes classes to give each car its implementation for the horn sounding. For uniqueness, you will create a version of the method for sounding the horn in both the Toyota and Mercedes classes.

The Car class has been preloaded for you.

Deze oefening maakt deel uit van de cursus

Introduction to Object-Oriented Programming in Java

Cursus bekijken

Oefeninstructies

  • Implement the soundHorn method in the Toyota class to print "sounds like a toyota".
  • Implement the soundHorn method inside the Mercedes class to print "sounds like a mercedes".
  • Inside the main method, call the soundHorn method on the myToyota instance and the soundHorn method on the myMercedes instance.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

public class Main {  
	static class Toyota extends Car {
        public Toyota() {
            super();
        }

        // Create soundHorn() for Toyota
        public ____ ____() {
            System.out.println("____");
        }
    }

    static class Mercedes extends Car {
    	// Create soundHorn() for Mercedes
        public void ____() {
            System.out.println("____");
        }

        public Mercedes() {
            super();
        }  
    }
    
    public static void main(String[] args) {
        Toyota myToyota = new Toyota();
        Mercedes myMercedes = new Mercedes();
        
        // Call "soundHorn" for myToyota and myMercedes
        ____.____();
        ____.____();
    }
    
}
Code bewerken en uitvoeren