We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Advanced
- Covariant Return Types
- Discussions
Covariant Return Types
Covariant Return Types
Sort by
recency
|
111 Discussions
|
Please Login in order to post a comment
Here is Covariant Return types solution in java - https://programmingoneonone.com/hackerrank-covariant-return-types-solution-in-java.html
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
//Complete the classes below class Flower { public String whatsYourName() { return "I have many names and types"; } }
class Jasmine extends Flower{ @Override public String whatsYourName() { return "Jasmine"; } }
class Lily extends Flower{ @Override public String whatsYourName() { return "Lily"; } }
class Region { public Flower yourNationalFlower() { return new Flower(); } }
class WestBengal extends Region{ @Override public Flower yourNationalFlower() { return new Jasmine(); } }
class AndhraPradesh extends Region{ @Override public Flower yourNationalFlower() { return new Lily(); } }
public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String s = reader.readLine().trim(); Region region = null; switch (s) { case "WestBengal": region = new WestBengal(); break; case "AndhraPradesh": region = new AndhraPradesh(); break; } Flower flower = region.yourNationalFlower(); System.out.println(flower.whatsYourName()); } }
attached image must be changed
one flower's name is Lotus
java conarient return type hacker rank solution