New Inner class

Report a typo

Inside your Vehicle create a new inner class Body.

Body should have the field String color, its constructor and the method void printColor for printing the color.

The output of the method printColor for a vehicle called Dixi with a red body will look like:

Vehicle Dixi has red body.

Remember about constructor in the outer class.

Please, don't use the access modifier private for a class or a method.

Write a program in Java 17
class Vehicle {

private String name;

// create constructor

class Engine {

void start() {
System.out.println("RRRrrrrrrr....");
}

}

// create class Body
}

// this code should work
class EnjoyVehicle {

public static void main(String[] args) {

Vehicle vehicle = new Vehicle("Dixi");
Vehicle.Body body = vehicle.new Body("red");
body.printColor();
}
}
___

Create a free account to access the full topic