• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Static invocation

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If in class A i declare one method go; which is static but in sub class i dnt declare same method which is not static. if i invoke non-static method using instance of sublcass will i get compilation error
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your example, there is no subclassing going on.... but yea, you are not allowed to define a non-static non-private method in the subclass, with the same name and signature, as a static method in the super class.

Henry
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java, many times you are allowed to do things that 'look wrong' but because they don't completely violate a rule, they're allowed to happen. This is one of those cases where you'd think you'd be able to bend the rules a bit and get away with it. But in fact, if you try and have a named method with the same name as a static method in the parent, you get slapped on the wrist by Java.

Indeed, this isn't overriding by definition. It's still wrong though.

-Cameron McKenzie
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And static methods cannot be overridden. Refer this
 
Greenhorn
Posts: 26
Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:In your example, there is no subclassing going on.... but yea, you are not allowed to define a non-static non-private method in the subclass, with the same name and signature, as a static method in the super class.

Henry



subclass has no access to superclass's static method. so, why there is restriction to define same method name as non-static
and if i define same method name with static in subclass it is compiling properly

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