• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

returning a object type

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello frnds,
I have one more observation
In kathy sierra chapter 2 and section named "Returning a value" it was stated
"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".
That is fine.But my point is we can even return any object type that can be explicitly casted to declared return type subjected to the following condition.
class Parent{
public void testMe(){

}
}

class Child extends Parent{
public Child sample(){
Parent parent = new Child();
System.out.println("sample execute");
return (Child)parent;
}
}

public class testMe{
public static void main(String[] args) {
Child child = new Child();
System.out.println("child method executed without exception"+child.sample());
}
}

Regards
Sri
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In kathy sierra chapter 2



Which one ? She has written many books .
 
Sri Sanjana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SCJP Sun Certified
Programmer for Java 5
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sri Sanjana wrote:But my point is we can even return any object type that can be explicitly casted to declared return type subjected to the following condition.



You need to distinguish between object type and reference type. In your example your object type is Child, but your reference type is Parent. As your object type is Child then of course you can return it from a method whose declared return type is Child.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sri Sanjana wrote:... But my point is we can even return any object type that can be explicitly casted to declared return type subjected to the following condition.



Yes. But the actual object being casted (at runtime) should be of return type or any subtype of it (Child in this case), Or you'll get a ClastCastException.

(Got little late replying, Joanne has already replied )
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic