CodeGym /Courses /Java Core /Polymorphism Tasks

Polymorphism Tasks

Java Core
Level 2 , Lesson 2
Available

"Hello, Amigo! Here are a couple hundred tasks on polymorphism for you."

"Huh? A hundred more exercises?!"

"I'm joking. Just ten. I mean, five."

3
Task
Java Core, level 2, lesson 2
Locked
Whales and cows
In real life you would never confuse a whale and a cow. Sea cows (manatees) are sometimes encountered in Terranian waters, and that's not all you'll see on the planets of the Commonwealth! That said, in programming you can do whatever you want. Correctly write a getter for the Whale class, so that the program displays the phrase, "I'm not a cow. I'm a whale."
3
Task
Java Core, level 2, lesson 2
Locked
Whales, the descendants of cows
In Java, method overriding is a very powerful tool. So we're going to begin to master it immediately through practice. Suppose we have a whale, a descendant of cows. Don't ask us how it happened... let's just say it's a family matter. Override the whale getter so that it doesn't display a message about being a cow.
3
Task
Java Core, level 2, lesson 2
Locked
Render to Caesar the things that are Caesar's
There is no doubt whatsoever that cats and dogs are animals. And their descendants must also be animals. But not just any animals. They should be very specific types of animal - cats and dogs. Don't you agree? In that case, override the getChild method in the Cat and Dog classes, so that cats give birth to cats and dogs to dogs.
3
Task
Java Core, level 2, lesson 2
Locked
Whether it's a bird or a lamp
What do birds and lamps have in common? The fact that birds and lamps can both be objects of their respective classes. And objects can be passed into methods as arguments. In this task, we are going to write a method that determines the type of the object passed to it, and then displays the appropriate message. Cat, Dog, Bird, and Lamp.
3
Task
Java Core, level 2, lesson 2
Locked
Animal identification
Napoleon, a cat breeder, believes that it is vitally important to know how to distinguish between types of animals. Let's help Napoleon out. We'll write a method that determines the class of the object passed to it, and returns one of the following results: "Cow", "Whale", "Dog", or "Unknown Animal".
Comments (17)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Krzysztof Kosała Level 21, Wejherowo, Poland
10 April 2024
The last one:

    public static String getObjectType(Object o) {
        return o instanceof Cow || o instanceof Whale || o instanceof Dog ? o.getClass().getSimpleName():"Unknown Animal";
    }
Thomas Level 13, Scottsdale, United States
16 January 2024
I'm not a cow. I'm a whale.
Alex Tor Level 31, Bulgaria
3 November 2022
an alternative way for 4th task:

        String result = o.getClass().getName().toString();
        System.out.println(result.substring(result.indexOf("$")+1));
Ashish RajAnand Level 13, Bhilai , India
10 May 2020
I glad .
Ruslan Skaldin Level 33, Tashkent, Uzbekistan
24 December 2019
It gives good explanation when we can use "instanceof" operator
Alexandru Ovcinicov Level 15, London, United Kingdom
25 September 2019
Moving ye
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
30 August 2019
it's not difficult yet, and i'm waiting a miniprojects as soon as can be it.
holybible Level 28, Plovdiv, Bulgaria
26 August 2019
sweet moving on
Krisztian Level 24, Budapest, Hungary
5 June 2019
If the task would be only 4, it could be a classic robot joke. you know, 100 in binary means 4. You get it? :D
Hossein Shams Level 1, Atlanta, United States
15 July 2019
or maybe he could say he's got 101 tasks for us. hahaha
Darko Jakimovski Level 18, Kriva Palanka, Macedonia, The Former Yugoslav Republic of
24 April 2019
God damn that improved my knowledge