Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Method Overriding

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code compiles successfully, why?
As whatever i know if sub class overrides a super class method then the return type of overriding method must be same as what of overridden method of super class (Compile time error otherwise). Please help
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

swaraj gupta wrote:

This code compiles successfully, why?
As whatever i know if sub class overrides a super class method then the return type of must be same as what in super class (Compile time error otherwise). Please help



Its using CoVariant Return type. You can return either the same type or sub class of that type. So in this case Foo extends SuperFoo, so where ever a SuperFoo instance is expected we can assign a Foo instance.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Swaraj, Could you please set the status as Yes for "Allow other users to send private messages:" in your My Profile page. I want to send a Private Message (PM)
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks mohammed sanaullah,

just one more thing to ask...i have tried this snippet and found that along with what you have said
we can return null as well from the overriding doStuff method of class Foo, may i have the reason for this.. ...
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

swaraj gupta wrote:i have tried this snippet and found that along with what you have said
we can return null as well from the overriding doStuff method of class Foo, may i have the reason for this.. ...


null can be type casted into any reference type, so you can return null from any method which returns an object i.e. anything except a primitive...
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ...
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Foo is-a SuperFoo.

Therefore when you're returning a foo, you're returning something that is-a superfoo. Aren't you?
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!