jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP for Java 6 book by Sierra and Bates covariant return code example question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP for Java 6 book by Sierra and Bates covariant return code example question" Watch "SCJP for Java 6 book by Sierra and Bates covariant return code example question" New topic
Author

SCJP for Java 6 book by Sierra and Bates covariant return code example question

alexandru costea
Greenhorn

Joined: May 18, 2010
Posts: 4
I've found this code in the book used to illustrate covariant method returns:



Now this would show in my interpretation that you can return a subtype of the method return type that subtype being Bear. But since the point, as I see it, is to be able to return a subtype that will be put in a supertype reference (an Animal holding a Bear object) how would this make any sense in the real world as I will not be able to instantiate Animal?

Thank you,
Alex.
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

Hi Alexandru, welcome to the ranch!
I am not sure I understood your question completely! You cannot instantiate an abstract class, but you can instantiate the concrete sub class, which is what the above example is showing!
Here is a class which implements an interface like the above code!




SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
alexandru costea
Greenhorn

Joined: May 18, 2010
Posts: 4
This example is from the book and I am puzzled as to its usefulness regarding polymorphism. Let me try and explain myself better.

This is a piece of code that explains my point.


So in this example I can take full use of polymorphism maybe with an array(herd) of Animal references that hold Animals as well as Bears returned by the go function or created or returned otherwise thus giving me the opportunity to call a function that exists in Animal and is overridden in Bear in a polymorphic way. The key point is that I can use the Animal class polymorphic as opposed to the example in the book.



Here I won't be able to instantiate an Animal object so I won't be able to to hold a Bear object into an Animal reference var. So it occurs to me that this is a faulty example because the method return type is Animal (which can never be instantiated) but the return object will always be a Bear so I will always use a Bear reference to hold the return value and thus making Animal as the method return type seem useless.

What I am asking is if I am right and the example provide in the book while being valid in syntax it doesn't really explain the issue at hand to the full extent. The thing is that while it illustrates the covariant return methods the example would never be useful in a real life endeavor. I mean since you can't have an instance of Animal why would you ever have it as a return type for a method.

If we were talking about an interface I think it would actually make a lot of sense but I don't get the point of the return type being abstract.

Thank you,
Alex.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Hi Alex!

...but the return object will always be a Bear so I will always use a Bear reference to hold the return value and thus making Animal as the method return type seem useless.


You're right--in your example, it doesn't make a difference whether go() returns an Animal or a Bear because, looking at the method body, we know it will always return a Bear. But what if you didn't know what the method was returning?



In this case, we can never be sure what getMostPopularAnimal() will return because the classes that extend Zoo can return any kind of Animal.


SCJP 6 || SCWCD 5
alexandru costea
Greenhorn

Joined: May 18, 2010
Posts: 4
Hi Michael,

the question though is why on earth would you choose to have the return type Animal when you will never have an instance of Animal created and will always return a Bear. I am saying that the example is faulty because in real life you will never ever find it useful. As I have written in in my first sequence of code class Animal is no longer abstract so it makes sense to have it as a return type whether you return an Animal or a Bear because you would take advantage of polymorphism.

In the case of the code in the book it would make no sense to ever put in a return type that you know will never occur. I mean you would put a return type and then be able to return subclasses of the return type for its usefulness but its useless having a piece of code, that can allow a variation, that you know will never happen.

It's like doing something just for the sake of doing it. The thing that I wanted is for someone to tell me that yes that piece of code could very well be changed to a return type Bear since it will never be able to return an Animal which is abstract and cannot be instantiated. I understand what it's trying to portray but I think it would have done it better with a non abstract class. It's legal this way too but it might have helped to have both pieces of code.

Thank you for your help. I think that that's just it but I wanted someone to confirm. So can you confirm my hypothesis? :P

Alex.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello Alex,

I apologize, i haven't read the whole thread, but skimmed through it and only got a fair idea that you want to know
why Animal has been put as the return type when it is abstract and not useful.

Well, i think so, have you heard something like 'Programming to an interface'. Please have a look at Strategy Pattern specially.
In this way, you are hiding the implementation from the user. Lets say your class was not abstract but an interface for suppose.
We always promote Programming to Interfaces, because they hide your implementation details. The people who are using your
application will never know what concrete implementation you are using.

Do you know java.util.Calendar is even an abstract class?

Hope this helps,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
While this doesn't completely answer the question, keep in mind that the example you cite from the book is inside an "exam watch". What this means is that this isn't necessarily a practical example, for the real world, it's more about a 'trick' you might encounter on the exam.

hth,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
alexandru costea
Greenhorn

Joined: May 18, 2010
Posts: 4
Thank you very much Bert that was the answer I was looking for. I knew it is legal(& good to watch for at the exam) and regarded it as such but it was the practical use that I wanted a confirmation for.

Alex.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP for Java 6 book by Sierra and Bates covariant return code example question