wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method can access in a subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static method can access in a subclass" Watch "Static method can access in a subclass" New topic
Author

Static method can access in a subclass

dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27
Hi All,

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....

Any help??

Thanks,
Dil.

Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
See my comments in code below...try to code and learn in many ways not only one way, the exam will test you on that.


Hope it clear up your confusion.




OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27
Hi,

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 ??

I do not understand...

Thanks,
Dil.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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.

dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27
Hi Tommy,

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.

is it??

Regards,
Dilan.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
I let you decided and tell me what do you think.....
dilan alex
Greenhorn

Joined: Sep 17, 2011
Posts: 27
Tommy Delson wrote:I let you decided and tell me what do you think.....


Hi tommy,

Yes yes...
"Static methods are inherited but cannot be overridden".

that is what I got to know by now.

Thank for support,
Dilan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method can access in a subclass
 
Similar Threads
static int not incrementing, constructor not called.
Guess The result
Instance of Operator
Need help on inheritance...
Overriding/overloading