Covariant Return Type in Java with Example - Java @ Desk

Sunday, June 29, 2014

Covariant Return Type in Java with Example

Covariant Return Type in Java with Example

In Java5, we have feature called covariant return type. Prior to Java5, we could not change the return type of the overridden method. It means that if you override the method in the sub class you need to downcast it to the subclass type.

The covariant return type allows narrowing down return type of the overridden method. This feature will help to avoid down casting on the client side. It allows programmer to program without the need of type checking and down casting. The covariant return type always works only for non-primitive return types. For primitive return types you will get a compiler error. It allows programmers to maintain backward compatibility.

The Clone method will return the object where we need to downcast the Object to the subclass type. After Java5, we can override the clone method to return the object of subclass type.

Consider the scenario:
1) We have two types of manufacturers two wheeler & four wheeler.
2) Based on the customer requirement if the user wants to purchase two wheeler the overridden method will return the bike object. Here the method getVehicleType will return the Bike object instead of vehicle object (Bike implements the Vehicle Class). i.e. returns the object of subclass instead of superclass.
3) Similarly the clone method in the bike class will return the object of bike instead of object of the class Object.
4) The same rule applies to the four wheeler class too.


Vehicle.java

public interface Vehicle {

 public void makeVehicle(String bikeName, String ownerName, int regNo);

}


Car.java

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;
 }

}


Bike.Java

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();
 }
}


Manufacture.java



public interface Manufacture {

 public Vehicle getVehicleType();

}


TwoWheeler.java

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();
 }

}


FourWheeler.java

public class FourWheeler implements Manufacture {

 @Override
 public Car getVehicleType() {
  // TODO Auto-generated method stub
  returnnew Car();
 }
}


ManufactureImpl.java

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