• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static method can access in a subclass

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I let you decided and tell me what do you think.....
 
dilan alex
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic