Inheriting the car class
With inheritance, we can create relationships between Classes and share code, avoiding needless repetition. You will make the Toyota class inherit all that we have created from the Car class, and to complete the inheritance, you must call the base class's constructor.
The Car class from earlier exercises has been preloaded for you.
Bu egzersiz
Introduction to Object-Oriented Programming in Java
kursunun bir parçasıdırEgzersiz talimatları
- Complete the inheritance of the
Toyotaclass from theCarclass using theextendskeyword. - Call the constructor of the
Carclass from within theToyotaconstructor using thesupermethod. - Pass the correct matching parameters to the
supermethod from the parameters of theToyotaconstructor. - Create an instance of
ToyotacalledmyToyotawith thecolorbeing"black", themodelbeing"yaris", and theyearbeing2014.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
public class Main {
// Enable the "Toyota" class to inherit from "Car"
static class Toyota ____ ____ {
public Toyota(String color, String model, int year){
// Call the "Car" constructor using "super()"
____(____, ____, ____);
}
}
public static void main(String[] args) {
// Create "myToyota" instance of Toyota
____ myToyota = new Toyota("____", "____", ____);
System.out.println(myToyota.model);
}
}