Covariant Return Types HackerRank solution

Covariant Return Types HackerRank solution
Covariant Return Types HackerRank solution

Java allows for Covariant Return Types, which means you can vary your return type as long you are returning a subclass of your specified return type.

Method Overriding allows a subclass to override the behavior of an existing superclass method and specify a return type that is some subclass of the original return type. It is best practice to use the @Override annotation when overriding a superclass method.

Implement the classes and methods detailed in the diagram below:

image

You will be given a partially completed code in the editor where the main method takes the name of a state (i.e., WestBengal, or AndhraPradesh) and prints the national flower of that state using the classes and methods written by you.

Note: Do not use access modifiers in your class declarations.

Resources
Covariant Return Type
Java Covariant Type

Input Format

The locked code reads a single string denoting the name of a subclass of State (i.e., WestBengalKarnataka, or AndhraPradesh), then tests the methods associated with that subclass. You are not responsible for reading any input from stdin.

Output Format

Output is handled for you by the locked code, which creates the object corresponding to the input string’s class name and then prints the name returned by that class’ national flower’s whatsYourName method. You are not responsible for printing anything to stdout.

Sample Input 0

AndhraPradesh

Sample Output 0

Lily

Explanation 0

An AndhraPradesh object’s yourNationalFlower method returns an instance of the Lily class, and the Lily class’ whatsYourName method returns Lily, which is printed by the hidden code checker.

Code solution:

//Covariant Return Types HackerRank solution
class Flower {
    String whatsYourName() {return "I have many names and types";}
}
class Jasmine extends Flower {
    @Override
    String whatsYourName() {return "Jasmine";}
}
class Lily extends Flower {
    @Override
    String whatsYourName() {return "Lily";}
}
class Region {
    Flower yourNationalFlower() {return new Flower();}
}
class WestBengal extends Region {
    @Override
    Flower yourNationalFlower() {return new Jasmine();}
}
class AndhraPradesh extends Region {
    @Override
    Flower yourNationalFlower() {return new Lily();}
}
//Covariant Return Types HackerRank solution

This Java code demonstrates the concept of covariant return types, which is when a subclass overrides a method defined in its superclass, and the subclass method returns a subtype of the type returned by the superclass method. Let’s break down the code and explain each part:

  1. Flower class:
    • This is the superclass that provides a method whatsYourName() which returns a string representing the name of the flower.
  2. Jasmine and Lily classes:
    • These are subclasses of Flower and they override the whatsYourName() method to return specific names for jasmine and lily flowers respectively.
  3. Region class:
    • This class provides a method yourNationalFlower() which returns a Flower object.
  4. WestBengal and AndhraPradesh classes:
    • These are subclasses of Region and they override the yourNationalFlower() method.
    • In WestBengal, the method returns a Jasmine object, and in AndhraPradesh, it returns a Lily object.

The approach used in this code is as follows:

  • The Flower class acts as the base class providing a common behavior for flowers.
  • Jasmine and Lily classes specialize the behavior for specific types of flowers by providing their names.
  • The Region class acts as a base class for defining regions and their national flowers.
  • WestBengal and AndhraPradesh classes specialize the behavior for specific regions by returning the national flower for their respective regions. They override the yourNationalFlower() method to return the specific type of flower found in each region.

Overall, this code demonstrates the usage of covariant return types where the subclass methods return more specific types than the superclass methods they override, allowing for more flexibility and specialization in the implementation.

Disclaimer: This problem is originally created and published by HackerRank, we only provide solutions to this problem. Hence, doesn’t guarantee the truthfulness of the problem. This is only for information purposes.

Leave a Comment