Create a sub class of Car class and name it as Truck. The Truck class has the following fields and methods.
◦intweight;
package q179433;
public class Q179433 {
    class Car {
        private int speed;
        private double regularPrice;
        private String color;
        public Car(int speed, double regularPrice, String color) {
            this.speed = speed;
            this.regularPrice = regularPrice;
            this.color = color;
        }
        public void displayInfo() {
            System.out.println("Speed: " + speed);
            System.out.println("Regular price: " + regularPrice);
            System.out.println("Color: " + color);
        }
    }
    class Truck extends Car {
        private int weight;
        public Truck(int Speed, double regularPrice, String color, int weight) {
            super(Speed, regularPrice, color);
            this.weight = weight;
        }
        @Override
        public void displayInfo() {
            super.displayInfo();
            System.out.println("Weight: " + weight);
        }
    }
    Truck truck = new Truck(150, 36000, "Red", 23000);
    public static void main(String[] args) {
        Q179433 q179433 = new Q179433();
        q179433.truck.displayInfo();
    }
}
Comments