Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello .
I am reading a book by Katherine Sierra and Bert Bates the is a statement i read but i didn't understood it :
In a method with an object reference return type, you can return any
object type that can be implicitly cast to the declared return type.

can anyone explain to me this statement.
Posted
Updated 22-Nov-11 0:14am
v2

1 solution

Hi,

A cast a conversion from a type to another.
Iimagine we have these elements :
- Class B that inherits from A.
- Functions F1 return a A class type object.


Inside F1 you handle ojects of type B and you want to return it as function return value.
To do that you need to cast B to A ( it is called UP-CASTING) then you can return it.


Java
B objectB = new B()
...
retObject = (A) objectB;
return retObject;



Hope this helps
 
Share this answer
 
v2
Comments
hamzah1 22-Nov-11 4:44am    
Thanks Bro it helps me :D
elgaabeb 22-Nov-11 4:48am    
You're welcome Bro :) if you like my solution vote it by stars ;))

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900