Solution Review: Playing with Cars and Engines!
This lesson provides the solution to the challenge, "Playing with Cars and Engines!" with an explanation.
We'll cover the following...
We'll cover the following...
Solution
Press + to interact
Java
// Car classclass Car {// Declaring data members and methodsprivate int id;private String model;private String color;public void carFeatures() { // Function to print out car featuresSystem.out.println("Car Model: " + model);System.out.println("Car Color: " + color);}public void setModel(String model) {this.model = model;}public void setColor(String color) {this.color = color;}}// Toyota Class, which is a child class of Car class.class Toyota extends Car {// Inherits all properties of Car classpublic void setStart() {// Declaring an engine object and calling the start() function of the engine.ToyotaEngine engine = new ToyotaEngine();engine.start();}}// Engine classclass ToyotaEngine {// This function simply prints out on screen that Engine has been started!public void start() {System.out.println("Engine has been started.");}// This function simply prints out on screen that Engine has been stopped!public void stop() {System.out.println("Engine has been stopped.");}}class Main {public static void main(String[] args) {// Declaring and initializing Toyota objectToyota t = new Toyota();t.setModel("Fortuner");t.setColor("Silver");t.carFeatures();t.setStart();}}
Explanation
- Line 25: Extended
Toyota
class fromCar
class.