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.


hackerrank covariant return types solution in java


Covariant return types problem solution in java | HackerRank

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 Lotus extends Flower{
    @Override
    String whatsYourName(){
        return "Lotus";
    }
}

class State{
    Flower yourNationalFlower(){
        return new Flower();
    }
}
class WestBengal extends State{
    @Override
    Jasmine yourNationalFlower(){
        return new Jasmine();
    }
}
class Karnataka extends State{
    @Override
    Lotus yourNationalFlower(){
        return new Lotus();
    }
}
class AndhraPradesh extends State{
    @Override
    Lily yourNationalFlower(){
        return new Lily();
    }
}

Second 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 Lotus extends Flower {
    @Override
    String whatsYourName() {
        return "Lotus";
    }
}

abstract class State {
    abstract Flower yourNationalFlower();
}

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();
    }
}