What are the covariant return types in java? Please give examples one by one
In Java, covariant return types allow a method in a subclass to override a method in a superclass and return a type that is a subclass of the return type declared in the superclass method. This feature was introduced in Java 5 and is particularly useful in object-oriented programming, as it allows for more specific return types while maintaining polymorphism.
Let's start with a simple example involving a superclass and a subclass.
class Animal {
Animal makeSound() {
System.out.println("Animal makes a sound");
return new Animal();
}
}
class Dog extends Animal {
@Override
Dog makeSound() { // Covariant return type
System.out.println("Dog barks");
return new Dog();
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.makeSound(); // Outputs: Dog barks
}
}
Animal
class has a method makeSound()
that returns an Animal
.Dog
class extends Animal
and overrides the makeSound()
method to return a Dog
instead of an Animal
.Dog
is a subclass of Animal
, demonstrating covariant return types.Covariant return types can also be used with interfaces.
interface Shape {
Shape getShape();
}
class Circle implements Shape {
@Override
Circle getShape() { // Covariant return type
return new Circle();
}
}
class Square implements Shape {
@Override
Square getShape() { // Covariant return type
return new Square();
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle();
Shape square = new Square();
System.out.println(circle.getShape().getClass().getSimpleName()); // Outputs: Circle
System.out.println(square.getShape().getClass().getSimpleName()); // Outputs: Square
}
}
Shape
with a method getShape()
that returns a Shape
.Circle
and Square
classes implement the Shape
interface and override the getShape()
method to return their respective types.Let's look at a more complex example with multiple levels of inheritance.
class Vehicle {
Vehicle getVehicle() {
return new Vehicle();
}
}
class Car extends Vehicle {
@Override
Car getVehicle() { // Covariant return type
return new Car();
}
}
class SportsCar extends Car {
@Override
SportsCar getVehicle() { // Covariant return type
return new SportsCar();
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new SportsCar();
System.out.println(vehicle.getVehicle().getClass().getSimpleName()); // Outputs: SportsCar
}
}
Vehicle
, Car
, and SportsCar
.getVehicle()
method, returning a more specific type.Covariant return types in Java allow subclasses to override methods and return a more specific type than the one defined in the superclass. This feature enhances the flexibility and expressiveness of Java's type system, especially in the context of inheritance and polymorphism.