I just finished the task and review it, I found out that there is a Creation of Selectable interface in the main, how was that even possible? i think you can't create an object of an interface
Confused of what's happening
Under discussion
Comments (20)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
31 May 2021, 22:16
This code means the followings:
1) We have created a new Food object.
The Food object contains these two methods:
onSelect() { System.out.println("The food was selected"); }
onEat() { System.out.println("The food was eaten"); }
2) We put this Food object into a variable which type is Selectable.
We can easily store an object in a variable which type is above our object on the inheritance chain.
If our object implements an interface, then that interface is above the object on the inheritance chain.
The interface is a bit like a parent class.
3) There is one very important consequence if we store an object in a different type of variable than the type of the object:
The variable selectab1 can only be used to access methods that are defined in the Selectable interface.
Specifically:
+3
John
1 June 2021, 03:39
oh but why did you say at first that the object cantains two methods(selectable(),onEat()) and ends up an error when trying to call the onEat method?
0
John
1 June 2021, 03:44
And what basically happens here Food newFood = (Food) selectable
0
Thomas
1 June 2021, 06:05
a little program for you:
We have a class hierarchy here. SeaPlane inherits from Plane. Plane implements an interface and can fly. The SeaPlane implements an interface, too, and can swim. In addition to that we have a class (Airline) where we can test things. Here we add some static methods that take arguments of certain interfaces or classes.
What you can see now is, that if you cast to a certain type, the object gets treated as if it is that type. If you cast a SeaPlane to a CanFly, you treat is as a CanFly and that just can fly. No other methods available for CanFly. To get all functionality back again, you'd need to cast the reference variable back to SeaPlane. Then you can use the interface (publically available set of methods) of the SeaPlane and that can swim and fly.
There's a lot more involved. For a better understanding I suggest you search the web for some tutorials on oop and polymorphism (that isn't involved here). 0
Gellert Varga
2 June 2021, 22:20
@John "why did you first say that the object cantains two methods(selectable(),onEat()) and ends up with an error when trying to call the onEat method?"
The object is an impalpable thing. It exists somewhere in memory. You can only work with it through the variable assigned it.
You will only ever see the object as the variable that stores its reference.
The variable used to access the object is like a sunglasses. If you use pink glasses, all objects will appear pink. But they are not pink in real.
See also the attached image.
+1
Gellert Varga
2 June 2021, 23:01
@John: "And what basically happens here:
Food newFood = (Food) selectable;"
See the attached picture:
So, both of selectab1 and food2 variables are pointing to the same Food object, but with the one you will be able to access the onEat() method of the object, but with the other one not:
selectab1.onEat(); // ERROR!
food2.onEat(); // It works!
+3
ImDevin
8 June 2021, 00:14
If this is indeed the case, I didn't know it. Thanx Gellert!
0
Gellert Varga
11 June 2021, 22:55
You're welcome:)
0
Thomas
31 May 2021, 12:35
The object created was a Food object. Just the reference variable pointing to that object is of Type Selectable. That is allowed and probably you use that already without knowing (List is an interface and eg. ArrayList has all the abstract methods implemented). The object always nees a concrete class, a class where all methods are implemented. The reference variable can be of the Type of an abstract class or an interface.
0
John
31 May 2021, 16:19
Thank you for the info thomas. the lesson about typecasting on reference is still unclear to me :3
0
John
1 June 2021, 06:45
When you upcast the interface (available methods) gets smaller so its called narrowing, and vice versa. is that right?
0
Thomas
1 June 2021, 06:53
Yes, as I tried to explain in the other thread
Try to imagine that with TV. I bet you already heared that but it's fitting really good. The object is a TV that you just control using a remote (that's the reference variable). The TV has a set of functions that you can access (with the correct remote). If you upcast, then you change remotes but that new remote has less buttons so you can't call all functions. However the TV is still a TV. Nothing has changed. Just the handle, the remote, the reference variable changes. That's the important thing you need to keep in mind. When casting the object itself doesn't change. You change the way how you can access that object.
+2
John
1 June 2021, 07:01
hmm "You change the way how you can access the object" what do you mean by this? the TV? or you mean change the way how can i use that object. sorry im confused but im inderstanding it now thank you for the help ;)
0
Thomas
1 June 2021, 10:34
The TV is the object with a fix set of fuctionality. The remote is the reference variable. Now you take the remote of a model with less functions and operate with that remote your TV. It works but with that remote you just can not invoke all functions. You treat your TV as if it is a smaller model. If you however take a remote of a TV with a richer feature set, then maybe weird things will happen (at least if you try that on objects that's the case). The TV doesn't have the functionality you send the commands to it.
0
Gellert Varga
2 June 2021, 21:29
@Thomas It's a very good example, i like it!!:)
0
Gellert Varga
2 June 2021, 21:44
@John:
upcasting = moving up on the inheritance chain = the number of available methods are decreasing = but it's other name is widening!! Not narrowing.
Because of the parent is a wider type than the child.
Because you can store any child-type object in a parent-type variable.
downcasting = moving down on the inheritance chain = the number of available methods are increasing = but it's other name is narrowing!! Not widening.
Because of the child type is a narrower type than the parent.
Because you can not store a parent-type object in a child-type variable.
+4
Thomas
2 June 2021, 21:56
;) Gellert, I liked the analogy as well. That's why I still remember it. And the explanation regarding narrowing/ widening that I know of I've already told John in his other thread (link above).
0
John
4 June 2021, 13:11
Thank you. you guys are awesome ;)
0
ImDevin
8 June 2021, 00:12
I compare the available methods to the scope of variables in a nested if/else statements(multiple). As you go into the nested if/else, the outer variables are available to the inner if/else's. However, as you come out of the inner if/else, the inner variables are not available to the outer if/else's.
0
Thomas
8 June 2021, 06:42
That's the default bahaviour of local variables in blocks (not only restricted to if - else structures). A local variables scope is within the braces it is declared in.
If you need a local variable you calculate inside an if-else block outside of that block then just declare it before entering that block.
0