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.
class JasmineNationalFlower implements NationalFlower {
@Override
public Flower yourNationalFlower() {
return new Jasmine();
}
}
class LilyNationalFlower implements NationalFlower {
@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();
NationalFlower nationalFlower = null;
switch (s) {
case "WestBengal":
nationalFlower = new JasmineNationalFlower();
break;
case "AndhraPradesh":
nationalFlower = new LilyNationalFlower();
break;
}
Flower flower = nationalFlower.yourNationalFlower();
System.out.println(flower.whatsYourName());
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Covariant Return Types
You are viewing a single comment's thread. Return to all comments →
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
// No need to change Flower class
interface NationalFlower { Flower yourNationalFlower(); }
class JasmineNationalFlower implements NationalFlower { @Override public Flower yourNationalFlower() { return new Jasmine(); } }
class LilyNationalFlower implements NationalFlower { @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(); NationalFlower nationalFlower = null;
}