Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Java . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Covariant Return Types-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Java
JAVA was developed by James Gosling at Sun Microsystems Inc in the year 1991, later acquired by Oracle Corporation. It is a simple programming language. Java makes writing, compiling, and debugging programming easy. It helps to create reusable code and modular programs.
Java is a class-based, object-oriented programming language and is designed to have as few implementation dependencies as possible. A general-purpose programming language made for developers to write once run anywhere that is compiled Java code can run on all platforms that support Java. Java applications are compiled to byte code that can run on any Java Virtual Machine. The syntax of Java is similar to c/c++.
Link for the Problem – Covariant Return Types – Hacker Rank Solution
Covariant Return Types – Hacker Rank 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:

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., WestBengal
, Karnataka
, 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 – Hacker Rank 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 */ } }
What i do not realize is actually how you’re not really much more well-liked than you might be right now. You are so intelligent. You realize thus significantly relating to this subject, produced me personally consider it from a lot of varied angles. Its like women and men aren’t fascinated unless it is one thing to accomplish with Lady gaga! Your own stuffs excellent. Always maintain it up!