• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Overriding doubt  RSS feed

 
Sushil Jamsandekar
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • 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.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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 .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!