Covariant Return Types

Sort by

recency

|

109 Discussions

|

  • + 0 comments
    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 = switch (s) {
                case "WestBengal" -> new WestBengal();
                case "AndhraPradesh" -> new AndhraPradesh();
                default -> new Region();
            };
            Flower flower = region.yourNationalFlower();
            System.out.println(flower.whatsYourName());
        }
    }
    
  • + 0 comments

    attached image must be changed

    one flower's name is Lotus

  • + 0 comments

    java conarient return type hacker rank solution

  • + 0 comments

    Covariant return types make method overriding flexible, like mapping states to flowers. Saw some cool Java insights on Top Digital Marketing Agencies Uk.

  • + 0 comments

    class Flower {

    String whatsYourName(){
    return "I have many names and types";
    

    } }

    class Jasmine extends Flower{ String whatsYourName(){ return "Jasmine"; } }

    class Lily extends Flower{ String whatsYourName(){ return "Lily"; } }

    class Region { public Flower yourNationalFlower(){ return new Flower(); } }

    class WestBengal extends Region{ public Jasmine yourNationalFlower(){ return new Jasmine(); } }

    class AndhraPradesh extends Region{ public Lily yourNationalFlower(){ return new Lily(); } }