Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA with JGRASP. Write a program to demonstrate the use of inheritance by creat

ID: 667225 • Letter: J

Question

JAVA with JGRASP.

Write a program to demonstrate the use of inheritance by creating and outputting 3 simple objects,
where the classes for the second and third objects inherit from the class for the first object.


The first object is a Vehicle with a number of passengers and colour. The second object is a Car with
additional number of doors. The third object is a Plane with a manufacturer and model number.


Note: do not inherit Plane from Car!


Use the provided Question1.java and Vehicle.java files. Use constructors and override methods as
appropriate.


Sample Output:
Blue 2 passengers
Black 4 passengers 4 doors
White 416 passengers Boeing 737

import java.util.*;

class Question1
{
public static void main ( String args [] )
{
System.out.println (new Vehicle (2, "Blue"));
System.out.println (new Car ("Black", 4, 4));
System.out.println (new Plane ("White", 416, "Boeing", 737));
}
}

public class Vehicle {
private int numPassengers;
private String colour;

Vehicle(int passengers, String colour) {
this.numPassengers = passengers;
this.colour = colour;
}

public String toString() {
return colour + " " + numPassengers + " passengers";
}
}

Explanation / Answer

import java.util.*;

class Vehicle {
    private int numPassengers;
    private String colour;

    Vehicle(int passengers, String colour) {
        this.numPassengers = passengers;
        this.colour = colour;
    }

    public String toString() {
        return colour + " " + numPassengers + " passengers";
    }
}


class Car extends Vehicle
{
    int doors;
// System.out.println (new Car ("Black", 4, 4));
     Car(String color,int passenger,int doors)
     {       
        super(passenger,color);
        this.doors=doors;      
    }
    @Override
    public String toString() {
        return super.toString()+" "+doors+" doors";
    }
    
}

class plane extends Vehicle
{
    //System.out.println (new Plane ("White", 416, "Boeing", 737));

    String manufact;
    int model_num;
    plane(String color,int doors,String manufactoror,int model_n)
    {
    super(doors,color);   
this.manufact=manufactoror;
this.model_num=model_n;
    }
   
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString()+" "+manufact+" "+model_num;
    }
   
   
}


public class Question1
{
   public static void main ( String args [] )
   {
      System.out.println (new Vehicle (2, "Blue"));
      System.out.println (new Car ("Black", 4, 4));
      System.out.println (new plane ("White", 416, "Boeing", 737));
   }
}