Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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 be inherited?

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


output is "lowhi". Static method can be inherited? Can anyone please explain?
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods are inherited but cannot be overriden they can be re- defined. Its Compiler error if you try to override a static method with non static method or override non static method with static method.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even final method is also inherited. But in K & B book it's written "only inherited methods can be override". But as we just saw you can inherit final & static methods but you cannot override them. Anyone explain?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dolly shah:
...in K & B book it's written "only inherited methods can be override"...


This is not the same as saying, "all inherited methods can be overridden."

In other words, for a method to be overridden, inheritance is a requirement, but it is not the only requirement. The method must also be a non-final instance (non-static) method.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

static methods aren't overridden, but redefined.

It is not called an override because they do not behave polymorphically as non-static methods do.


As I said, this is not called an override. If the method was non-static, the last line would produce a "B" because of polymorphism. For static method, only the variable type counts.
Therefore it is recommended not to call static methods on variables.


By the way, despite the fact that this is NO override, some behaviours are the same when comparing overriding and redefinition. For example you cannot make the static method in the child less visible than in the parent and you cannot throw broader exceptions.


final:
as in a non-static method where final does not allow overriding, in a static method final does not allow redefinition. When you make the method final in class A, class B will no longer compile.

Yours,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic