CodeGym /Courses /Java Core /Tasks about type casting

Tasks about type casting

Java Core
Level 4 , Lesson 4
Available

"Do you know what I think?"

"What's that, my friend?"

"You've almost reached my level. Soon you will be able to write programs yourself."

"Seriously, Diego?"

"Of course not, you whipper snapper. Ha-ha. You still need to learn and learn some more. But no whining. Figure this out!"

4
Task
Java Core, level 4, lesson 4
Locked
Building and School
There are ordinary buildings, and there are school buildings. Make one inherit the other, and think about the type of object the getSchool and getBuilding methods should return. Change null to a Building or School object.
8
Task
Java Core, level 4, lesson 4
Locked
Cats
Create an application for a cat census. First, ask the user to enter cat names. Then the program should create Cat objects with the appropriate names and display the result of cat.toString().
8
Task
Java Core, level 4, lesson 4
Locked
Food
We will create a menu framework for restaurants, more precisely—a function for selecting food. First, we implement the Selectable interface in the Food class, and the onSelect() method, which should display "The food was selected". Think about which methods can be called with the variable food and which with the variable selectable.
4
Task
Java Core, level 4, lesson 4
Locked
No mistakes
In this task, once again something is wrong. So you need to fix something. Specifically, set obj equal to an object that allows the main method to run error-free.
4
Task
Java Core, level 4, lesson 4
Locked
Player and Dancer
On Planet Terra, practically every dancer or player is a human. The same is true in this incomplete program you have in front of you. Take a look at what it does already, and then change the haveFun method so that it calls the play method for players and the dance method for dancers.
Comments (12)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Oliver Heintz Level 18, Mustang, United States
5 December 2020
I'm fumbling my way through these. I am able to figure things out with only a little researching. I don't think I fully understand the concepts, but I am understanding what works and what doesn't work. Maybe that is enough for now?
Chandan Thapa Level 22, Dubai, United Arab Emirates
25 November 2020
things are getting much clearer now!
Ntuthuko Xaba Level 18, Johannesburg, South Africa
5 April 2020
public static void selectableMethods(Selectable selectable) { selectable.onSelect(); } I'm a bit confused , I thought we could'nt create instances of Interfaces or Abstract classes, but this happened to be the solution to the 'Food' task. Would anyone care to clarify please.
Tara Rosenthal Level 18, Farmers Branch, United States
17 April 2020
Quote from a website on what is happening with the interface: "Once you create a Java Class which implements any Interface, the object instance can be referenced as an instance of the Interface. This concept is similar to that of Inheritance instantiation." Interfaces cannot be instantiated as objects themselves, but a class that implements them can store its object as a variable created from that interface. In other words, you can't do this: Selectable selectable = new Selectable(); But you can do this: Selectable selectable = new Food(); Then you can call the selectableMethods with the second variable.
Ntuthuko Xaba Level 18, Johannesburg, South Africa
17 April 2020
tnx
Ivan Level 22, Nope, Bulgaria
3 December 2019
"Do you know what I think?" "What's that, my friend?" "You've almost reached my level. Soon you will be able to write programs yourself." "Seriously, Diego?" "Of course not, you whipper snapper. Ha-ha. HAHAHA, that was a really cool one!
rydenfoo Level 20, Birmingham, UK
15 August 2019
Whoa! Some strange things I see here:

Selectable selectable = new Food();
Person person = new Player();
Interfaces can act as types as well? Have we seen this before? As we cannot create instances of interfaces or abstract classes, I assumed this was not possible...
Alexandros Kapriniotis Level 24, greece, Greece
29 August 2019
he is creating a new instance of food which is a class not an interface
Geethika medidi Level 18, Hyderabad, India
17 July 2019
https://www.geeksforgeeks.org/type-conversion-java-examples/ simplest explanation
Fa Yu Level 16, Tashkent, Uzbekistan
2 July 2019
good side is that tasks done easily, but bad side i didn't understand clearly what i did :)) just did it intuitively
Niranjan Soni Level 15, Bangalore, India
4 October 2018
Why it's not possible to call onEat() method on newFood object in method selectableMethods() while newFood object is a type of Food class as well.
Ștefan Marinescu Level 24, Iasi, Romania
20 March 2019
selectable is not of type food but of the type Selectable.You need to cast it to the type food to have acces to the method onEat. ((Food)selectable).onEat();