![]() If a superclass has a subclass reference variable, the compiler knows right away that there's "too many buttons on the remote control" and thus. There's not enough "buttons" on the remote to do everything, but the "buttons" it has will work. The compiler knows that a subclass can have a superclass reference variable. You're effectively trying to fit a superclass object into a subclass reference. well, what happens to methods that are Dog specific (begAtTable(), sniffWeirdThings(), obeyOwner())? Compiler says "NO WAY! There's not enough 'stuff' there" So, when you do this: After all, if you try to put an "Animal" inside a "Dog". The compiler KNOWS that a Child reference can't hold a Parent. But then the cast happens at runtime, there's an exception because the p2 object doesn't have all the "stuff" it needs to make it a Child object. I know it's weird." The compiler believes you. This will compile, because you're trying to tell the compiler "It's okay. ![]() It's legal, but 'p' will only be able to do Parent things. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |