Actually I just wanted to ask about this code snippet.
Here Prius Class extends Toyota class and as we know static methods are not overridden. But in main method I have created Prius object and calling breaking method -
Its called Toyota class breaking method. how does this happen ??
Static methods are not overridden ? is it?
this is the output i get
Vehecle Created....
Toyota Created....
Prius Created....
Toyota Breaking....
Thanks Tommy for your reply. But still I'm confused because,
Ok It has redefined breaking() method in Toyota class. But In Prius class it has not redefined.
In my Prius class no any method named breaking(). So how prius reference variable can access that method ??
You don't have to create Prius's reference instance variable in order to access breaking() method in Toyota class, try call "breaking()" alone in Prius class.
Why does it works? Simply Prius is inherited everything from Toyota plus Vehicle as well. In other word, Prius is under inheritance tree so, everything Toyota or Vehicle has Prius able to uses or access since it's under an Inheritance Tree.
Remember any super class such as Vehicle or Toyota implements an interface, the Prius class doesn't have to implement the interface unless it wants to. This call indirect inheritance from Vehicle or Toyota's classes.
Try code below in your main() method on your original code:
If you still not clear or confuse go back to Chapter 2 on Inheritance topic for more insight.
OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Ok It means All methods are inherited regardless of static, but we cannot override them(only static methods).
We can access these inherited static methods using object reference or by direct.