Covariant Return Types HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Covariant Return Types HackerRank Solution. This problem is a part of the HackerRank Java Programming Series.

Ezoicreport this adCovariant Return Types HackerRank Solution
Covariant Return Types HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Covariant Return Types HackerRank Solution

Ezoicreport this adObjective

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:

Covariant Return Types Hacker Rank Solution

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.

Covariant Return Types HackerRank Solution

package com.javaaid.hackerrank.solutions.languages.java.advanced;
public class CovariantReturnTypes {
  /**
   * main code starts from here** */
  class Flower {
    public String whatsYourName() {
      return "I have many names and types.";
    }
  }
  class Jasmine extends Flower {
    public String whatsYourName() {
      return "Jasmine";
    }
  }
  class Lily extends Flower {
    public String whatsYourName() {
      return "Lily";
    }
  }
  class Lotus extends Flower {
    public String whatsYourName() {
      return "Lotus";
    }
  }
  class State {
    Flower yourNationalFlower() {
      return new Flower();
    }
  }
  class WestBengal extends State {
    Jasmine yourNationalFlower() {
      return new Jasmine();
    }
  }
  class Karnataka extends State {
    Lotus yourNationalFlower() {
      return new Lotus();
    }
  }
  class AndhraPradesh extends State {
    Lily yourNationalFlower() {
      return new Lily();
    }
    /**
     * main code ends here  */
  }
}

Disclaimer: The above Problem (Covariant Return Types) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Java Annotations HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad