Tasks about type casting
Comments (12)
- Popular
- New
- Old
You must be signed in to leave a comment
Oliver Heintz
5 December 2020, 23:02
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?
+7
Chandan Thapa
25 November 2020, 13:55
things are getting much clearer now!
+2
Ntuthuko Xaba
5 April 2020, 14:16
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.
+2
Tara Rosenthal
17 April 2020, 19:55
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.
+13
Ntuthuko Xaba
17 April 2020, 20:18
tnx
0
Ivan
3 December 2019, 11:59
"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!
+11
rydenfoo
15 August 2019, 20:53
Whoa! Some strange things I see here:
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... +3
Alexandros Kapriniotis
29 August 2019, 09:30
he is creating a new instance of food which is a class not an interface
+5
Geethika medidi
17 July 2019, 06:17
https://www.geeksforgeeks.org/type-conversion-java-examples/
simplest explanation
+8
Fa Yu
2 July 2019, 09:59
good side is that tasks done easily, but bad side i didn't understand clearly what i did :)) just did it intuitively
+5
Niranjan Soni
4 October 2018, 21:52
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.
+1
Ștefan Marinescu
20 March 2019, 11:11
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();
+10