Vehicle.java
public interface Vehicle { public void makeVehicle(String bikeName, String ownerName, int regNo); }
public class Car implements Vehicle { String _bikeName; String _ownerName; int_regNo; @Override public void makeVehicle(String bikeName, String ownerName, int regNo) { // TODO Auto-generated method stub _bikeName = bikeName; _ownerName = ownerName; _regNo = regNo; } }
public class Bike implements Vehicle, Cloneable { String _bikeName; String _ownerName; int_regNo; Bike _clonedBike; @Override public void makeVehicle(String bikeName, String ownerName, int regNo) { // TODO Auto-generated method stub _bikeName = bikeName; _ownerName = ownerName; _regNo = regNo; } //returnthe Bike object instead of object of class Object. protected Bike clone() throws CloneNotSupportedException { _clonedBike = (Bike) super.clone(); return_clonedBike; // return super.clone(); } }
public interface Manufacture { public Vehicle getVehicleType(); }
public class TwoWheeler implements Manufacture { @Override //Here the overridden method returns the Bike object instead of Vehicle Objcet. public Bike getVehicleType() { // TODO Auto-generated method stub returnnew Bike(); } }
public class FourWheeler implements Manufacture { @Override public Car getVehicleType() { // TODO Auto-generated method stub returnnew Car(); } }
public class ManufactureImpl { /** * @param args * @throws CloneNotSupportedException */ public static void main(String[] args) throws CloneNotSupportedException { // TODO Auto-generated method stub TwoWheeler s1 = new TwoWheeler(); Bike b1 = s1.getVehicleType(); b1.makeVehicle("ThunderBird", "XXXX", 3456); Bike b2 = b1.clone(); b2._ownerName = "YYYY"; b2._regNo = 6789; System.out.println("Owner Name Of Bike1->" + b1._ownerName); System.out.println("Owner Name Of Bike2->" + b2._ownerName); FourWheeler f1=new FourWheeler(); Car c1=f1.getVehicleType(); c1.makeVehicle("Ford", "ZZZZ",9999); System.out.println("Owner Name Of Car->" + c1._ownerName); } }
No comments:
Post a Comment