/* This program demostrates an example for inheritance. We first define a class called Vehicle, which is a generic class. We then define the classes Car and Motorcycle, which inherit from the class Vehicle. We then define a Test class that instantiates the Car class. We also demonstrate how we can create an array of mixed Car and Motorcycle objects and use the Vehicle class as the reference. This is also an example of how one file can contain many classes. The name of the file should match the name of the class that contains the main method. */ import java.util.Scanner; //This is the base class or the superclass class Vehicle { int numWheels; double price; //default constructor public Vehicle() { numWheels = 0; price = 0; } //parameterized constructor public Vehicle(int n, double p) { numWheels = n; price = p; } //accessor methods public int getNumWheels() { return numWheels; } public double getPrice() { return price; } //mutator methods public void setNumWheels(int n) { numWheels = n; } public void setPrice(double p) { price = p; } //method to print the attributes public void print() { System.out.println("Number of wheels : " + numWheels + "\nPrice : "+price); } } //Derived Class, for a Car class Car extends Vehicle // the extends keyword here specifies inheritance { String model; boolean isAutomatic; //constructors. Both of them invoke the constructors of the superclass. public Car() { super(); model = "Ford"; isAutomatic = true; } // note here how the parameters include the ones passed to the superclass constructor public Car(int n, double p, String m, boolean i) { super(n,p); model = m; isAutomatic = i; } // accessor and mutator methods public String getModel() { return model; } public boolean getIsAutomatic() { return isAutomatic; } public void setModel(String m) { model =m; } public void setIsAutomatic(boolean i) { isAutomatic = i; } /*The print method is being overridden here. We use the super keyoword to also invoke the print for the superclass, so that all attibutes are printed. If we leave that out, only model and isautomatic will be printed. */ public void print() { super.print(); System.out.println("Model : "+model+"\nAutomatic Transmission? : "+isAutomatic); } } //Derived Class, for a Motorcycle class Motorcycle extends Vehicle // the extends keyword here specifies inheritance { String model; String type; //Please note how the two derived classes need not have the same attributes. //constructors. Both of them invoke the constructors of the superclass. public Motorcycle() { super(); model = "Yamaha"; type = "Sport"; } // note here how the parameters include the ones passed to the superclass constructor public Motorcycle(int n, double p, String m, String t) { super(n,p); model = m; type = t; } // accessor and mutator methods public String getModel() { return model; } public String getType() { return type; } public void setModel(String m) { model =m; } public void setIsAutomatic(String t) { type = t; } /*The print method is being overridden here. We use the super keyoword to also invoke the print for the superclass, so that all attibutes are printed. If we leave that out, only model and isautomatic will be printed. */ public void print() { super.print(); System.out.println("Model : "+model+"\nType : "+type); } } // This is a test class or a driver class that makes use of the classes we created above class TestInherit { public static void main(String [] args) { //We instantiate each class and invoke the print method Vehicle v1 = new Vehicle (4, 20000); // Create an arbitrary vehicle v1.print(); Car c1 = new Car (4, 14000, "Toyota", true); c1.print(); Motorcycle m1 = new Motorcycle (2, 10000, "HarleyDavidson", "Street"); m1.print(); //We create an array of the base class. Vehicle [] varray = new Vehicle[5]; //We can now create objects of the derived class and assign them to the base class references. boolean isCar; // We use this to ask the user if it is a car or a bike. //placeholder variables for user input int nw; double pr; String mod; boolean isAuto; String ty; Scanner in = new Scanner(System.in); for(int i=0;i<5;i++) { System.out.println("Is this a car? Enter true/false. : "); isCar = in.nextBoolean(); if(isCar == true) { System.out.println("Enter the number of wheels, price, model and if it is an automatic? : "); nw = in.nextInt(); pr = in.nextDouble(); mod = in.next(); isAuto = in.nextBoolean(); varray[i] = new Car(nw,pr,mod,isAuto); } else //if it's not a car, it must be a bike { System.out.println("Enter the number of wheels, price, model and type : "); nw = in.nextInt(); pr = in.nextDouble(); mod = in.next(); ty = in.next(); varray[i] = new Motorcycle (nw,pr,mod,ty); } } //now we invoke the print methods. for(int i =0; i<5;i++) varray[i].print(); } }