Clarifications on this topic. So for examples purposes Say I have:
public interface Gaming {
void play;
}
public abstract class Console implements Gaming {
void play() {}
}
public class Xbox extends Console {
void play() {}
}
public class XboxOne extends Xbox {
void play() {}
}
- I can never instantiate
Console
directly because it'sabstract
I can however instantiate console as an xbox
Console fav = new Xbox();
If all of the classes have a defined
play()
method. If I callfav.play();
it will look at the Xbox's play method, if it did not have a play method it would continue up the hierarchy until it found that method?Also if I did
((XboxOne)fav).play()
it would do the XboxOne's play method?, also is it true that I can only cast an object if it's lower in the hierarchy?If the
Xbox
class had agetGamerscore
method but the console didn't, would I be able to runfav.getGamerScore()
?
General Questions:
The type on the left of the = shows what class (most specific) java will look in when a method is called? (If it cannot find it there it will continue up the hierarchy until it finds the method?)
The type on the right just stands for the compile type. When compiling java will look and see if the method or data belongs to the compile type and if so everything is good? Java will not look at it's compile type anymore when it's running.
Casting just helps get past compile problems? Like if I want to use a certain method but my objects compile type is like an interface or abstract class or something, so I cast so the compiler doesn't complain when I try to access the runtime type's methods?
Please correct me if I've said anything wrong I just want to get all the rules clear in my head. Also if anyone has any helpful resources that would be great.
**I realize I did not use the gaming interface