/* This class demonstrates inheritance and dynamic binding. * We create a generic vehicle class and then write 2 derived classes Car and Boat. * Then, we create an array of Vehicle objects and dynamically set each object of the array to Car or Boat, depending on the user's choice. */ import java.util.Scanner; /* This class serves as the base class. It has 2 attributes, number of wheels, and color. * We write a default constructor, a parametrized constructor and accessor and mutator methods. * We also write a print method to print the details. */ class Vehicle { //Data attributes. We make them protected so that the derived classes have access to them. protected int numWheels; protected String color; //constructors public Vehicle() { numWheels = 4; color="blue"; } public Vehicle(int n, String c) { numWheels= n; color =c; } //accessors public int getNumWheels() { return numWheels; } public String getColor() { return color; } //mutators public void setNumWheels(int n) { numWheels=n; } public void setColor(String c) { color=c; } //print method public void print() { System.out.println("Number of Wheels: "+getNumWheels() + "\nColor: "+ getColor()); } } /* The Car class inherits from Vehicle. Thus, it already contains all the data attributes and the methods of the Vehicle class. * We add one more attribute - make. * We write a default and a parametrized constructor, and accessor and mutator methods for the new attributes. * We override the print method to print information about the car objects. */ class Car extends Vehicle { String make; public Car() { //super calls the constructors of the base class. It needs to be the first line of the method. super(); make = "Toyota"; } public Car(int n, String c, String m) { super(n,c); make =m; } public String getMake() { return make; } public void setMake(String m) { make =m; } //overriding the print method public void print() { System.out.println("Number of Wheels: "+getNumWheels() + "\nColor: "+ getColor() + "\nMake: " +getMake()); } } /* The Boat class inherits from Vehicle. Thus, it already contains all the data attributes and the methods of the Vehicle class. * We add one more attribute - hasSails. * We write a default and a parametrized constructor, and accessor and mutator methods for the new attributes. * We override the print method to print information about the boat objects. */ class Boat extends Vehicle { boolean hasSails; public Boat() { super(); hasSails = true; } public Boat(int n, String c, boolean h) { super(n,c); hasSails = h; } public boolean getHasSails() { return hasSails; } public void setHasSails(boolean h) { hasSails=h; } public void print() { super.print(); // You can call the superclass's method in the overridden method. System.out.println("It has sails: "+getHasSails()); } } class ManyVehicles { public static void main(String[] args) { int N; Scanner input = new Scanner(System.in); System.out.print("Enter the number of objects: "); N=input.nextInt(); //declare the array of base class objects. Vehicle [] vArray = new Vehicle [N]; for(int i=0;i