I’m kinda unsure about the following question:
If Launchable is a Java interface, what objects can be passed into the following method? What methods could be invoked on item inside this method?
public void prepareForLaunch (Launchable item) {
// some code
}
My current answer is:
From the above information, the only objects that can be passed into the method are objects that where instantiated as subclass types of the interface Launchable.(?) The methods that could be invoked on item inside the method would have to be public methods or protected methods within the same package. These methods would also have to be to be intended for a subclass of Launchable object since it is only in abstract and actual(concrete) classes where a method body’s definition can exist.
I was wondering if someone here can check my answer and add any suggestions. Thanks!
You can only pass in instances of classes that implement
Launchable(either directly, or by inheritance from a superclass). You can also pass innull.Inside of the method, you can call all the methods defined in
Launchable(and inObject).These methods would be defined in the
Launchableinterface, but implemented in the actual class (a fact that is guaranteed by the Java type system, which won’t let you have classes with incomplete interface implementations, those would need to be declared abstract and cannot be instantiated).If you need to call any other methods you need to know that the object in question also implements some other interface (or is of a given class), and do a typecast to that first.