package inheritance; import java.util.ArrayList; /** * * @author Mayelin */ public class Inheritance { public static void main(String[] args) { Bicycle bike_1 = new Bicycle(20, 10, 1); MountainBike bike_2 = new MountainBike(20, 10, 5, "Dual"); RoadBike bike_3 = new RoadBike(40, 20, 8, 23); /* The appropriate version of the printDescription method gets called at run time depending on the actual type of the object. */ bike_1.printDescription(); System.out.println("__________________"); bike_2.printDescription(); System.out.println("__________________"); bike_3.printDescription(); /* Example of Polymorphism: The following three reference variables are of type Bicycle, but bikeTwo and bikeThree reference objects of type MountainBike and RoadBike respectively. */ Bicycle bikeOne, bikeTwo, bikeThree; bikeOne = bike_1.copy(); bikeTwo = bike_2.copy(); bikeThree = new RoadBike(bike_3); /* The compiler only lets you call methods in the variable's data type (Bicycle class in this case) even if the object it references is of a subtype that has more methods. The compiler lets you call the printDescription method because it exists in the Bicycle class, which is the variable's data type. The appropriate version of the printDescription method gets determined at run time depending on the actual type of the object. */ bikeOne.printDescription(); bikeTwo.printDescription(); bikeThree.printDescription(); /* Example of a polymorphic array list. The ArrayList is declared to have elements of type Bicycle, but since MountainBike and RoadBike objects are subclasses of Bicycle (they are Bicycle objects), it's ok to add them too. */ ArrayList listOfBikes = new ArrayList(); listOfBikes.add(bikeOne); listOfBikes.add(bikeTwo); listOfBikes.add(bikeThree); // The following code iterates through the list of Bicycle objects, which may be of type MountainBike or RoadBike Bicycle bike; MountainBike mBike; RoadBike rBike; for( int i=0 ; i < listOfBikes.size(); i++ ) { bike = listOfBikes.get(i); // The compiler only let's us call methods of the Bicycle class unless we cast System.out.println("-------------------------"); System.out.println("Bike " + (i+1)); System.out.println("-------------------------"); bike.printDescription(); if( bike instanceof MountainBike ) { // cast into a MountainBike object so we may call the methods in that class mBike = (MountainBike)bike; System.out.println("Suspension for MountainBike: " + mBike.getSuspension()); } else if( bike instanceof RoadBike ) { // cast into a RoadBike object so we may call the methods in that class rBike = (RoadBike)bike; System.out.println("Tire width for RoadBike: " + rBike.getTireWidth()); } else { System.out.println("This is a Bicycle "); } } // Example of a polymorphic method for( Bicycle bike1 : listOfBikes ) System.out.println( getBikeInfo(bike1) ); } // This method has a Bicycle parameter, but it can be called with an argument of any type that's // a subclass of Bicycle. static String getBikeInfo(Bicycle bike) { bike.printDescription(); MountainBike mBike; RoadBike rBike; if( bike instanceof MountainBike ) { mBike = (MountainBike)bike; return "Suspension for MountainBike " + mBike.getSuspension() + "\n"; } else if( bike instanceof RoadBike ) { rBike = (RoadBike)bike; return "Tire width for RoadBike " + rBike.getTireWidth() + "\n"; } else { return "This is a Bicycle\n"; } } }