• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Method Overriding doubt

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In below code, ClassB is having method with same name as ClassA but different return type. As overriding requires return type should also be same, this method should act as different method. But it gives compile time error as "This instance method cannot override the static method from ClassA". Why cant it acts like a new method?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static methods cannot be overridden but can be hidden. Read Class Methods heading here

If you search either in this forum or the Java in General forum, you will get numerous answers.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sushil Jamsandekar wrote:...But it gives compile time error as "This instance method cannot override the static method from ClassA". Why cant it acts like a new method?


The simple answer is: because it can't. static methods can't be overridden; only masked.

And if both methods were instance methods you would still have a problem because methods with the same signature must return the same type (or a covariant one).

Winston

Edit: too slow .
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic