Use inheritance to implement the following classes A A Car t
Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called the number_of_engines it has. Add public methods to set and get the values of these variables. When an airplane is printed (using the toString method), its name, max_speed and number_of_engines are shown. C: Write a VehicleDemo.java class that does the following: 1- Creates an instance of a Car and an Airplane class. 2- Assign values to the name, speed, number_of_cylinders (for the Car object) and number_of_engines (for the Airplane object) variables. 3- Compares which vehicle goes faster and prints the result. 4- Prints the instances of the car and airplane classes.needed coding in java
Solution
Vehicle.java
public class Vehicle {
    //Declaring variables
    private String name;
    private int max_speed;
   
    //Parameterized constructor
    public Vehicle(String name, int max_speed) {
        super();
        this.name = name;
        this.max_speed = max_speed;
    }
   
    //Setters and getters
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getMax_speed() {
        return max_speed;
    }
    public void setMax_speed(int max_speed) {
        this.max_speed = max_speed;
    }
    @Override
    public String toString() {
        return \"Name=\" + name + \"\ Max Speed=\" + max_speed;
    }
   
    }
______________________
Car.java
public class Car extends Vehicle {
   //Declaring instance variables
    int no_of_cylinders;
  
    //parameterized constructor
    public Car(String name, int max_speed, int no_of_cylinders) {
        super(name, max_speed);
        this.no_of_cylinders = no_of_cylinders;
    }
   //getters and setters
    public int getNo_of_cylinders() {
        return no_of_cylinders;
    }
   public void setNo_of_cylinders(int no_of_cylinders) {
        this.no_of_cylinders = no_of_cylinders;
    }
   //toString() method is used to display the contents of an object inside it
    @Override
    public String toString() {
        System.out.println(\"\ ** CAR **\");
        System.out.println(super.toString());
        return \"No of cylinders=\" + no_of_cylinders;
    }
 }
_________________________
Airplane.java
public class Airplane extends Vehicle {
    //Declaring instance variables
    int no_of_engines;
   //parameterized constructor
    public Airplane(String name, int max_speed, int no_of_engines) {
        super(name, max_speed);
        this.no_of_engines = no_of_engines;
    }
   //getters and setters
    public int getNo_of_engines() {
        return no_of_engines;
    }
   public void setNo_of_engines(int no_of_engines) {
        this.no_of_engines = no_of_engines;
    }
   //toString() method is used to display the contents of an object inside it
    @Override
    public String toString() {
        System.out.println(\"\ ** AIRPLANE **\");
        System.out.println(super.toString());
        return \"No of engines=\" + no_of_engines;
    }
   
}
______________________
VehicleDemo.java
public class VehicleDemo {
   public static void main(String[] args) {
        //Creating the references of Vehicle class
        Vehicle car,airplane;
       
        //creating the Car class object
        car=new Car(\"Ferrari\",300,12);
       
        //creating the Airplane class object
        airplane=new Airplane(\"Boeing 747-8\",700,4);
       
       
        //Calling the toString() method on the Car class object
        System.out.println(car.toString());
       
        //Calling the toString() method on the Airplane class object
        System.out.println(airplane.toString());
}
}
____________________________
output:
 ** CAR **
 Name=Ferrari
 Max Speed=300
 No of cylinders=12
** AIRPLANE **
 Name=Boeing 747-8
 Max Speed=700
 No of engines=4
________________Thank You



