File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can we return a interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can we return a interface?" Watch "How can we return a interface?" New topic
Author

How can we return a interface?

Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
It has been said in K & B that

the return type of methods like

1)descendingSet()
or
2)descenfingMap()

are
NavigableSet and NavigableMap respectively.

I don't understand, how can return type be an interface(no instances of interface are possible,right?)?

and what does it mean


It means that the actual instance is of TreeSet, however since there is no reference variable of other type, why is the casting required?

I mean to say that consider the following example:


Here the dog object is being refered by Animal type ref. variable so it must be casted to Dog, but the problem im talking about, there is no ref. variable, there is this instance and since it is an instance of TreeSet, why casting is required.

The return type of the method is NavigableSet, what does that mean?
Does that mean that the instance that is returned from the method is ofcourse a TreeSet but it is being casted to NavigableSet before being send something like this:


Waiting for enlightenment....
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The headSet() method returns a sorted set -> http://download-llnw.oracle.com/javase/6/docs/api/java/util/NavigableSet.html#headSet%28E%29

TreeSet is a sorted set and the subclass happens to be a TreeSet at runtime, thus the casting works. The return type means, any subclass instance of type SortedSet can be returned to the caller


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Dog d = (Dog)a; here a is not a Dog. so you need explicit casting to satisfy the compiler
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
Suppose there is a method name() whose return type is an Animal where Animal is an interface,

so if i write

then according to you it won't compile, since the return type is an Animal, it can be any implementor class of Animal, and thus casting is essential?

Is my interpretation correct?

And the same applies to headSet() too, its return type is SortedSet, so it can return any implementor class of SortedSet(including TreeSet)
thus explicit casting is required?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Prateek Rawal wrote:
then according to you it won't compile, since the return type is an Animal, it can be any implementor class of Animal, and thus casting is essential?

yes, you need explicit casting.
Prateek Rawal wrote:
And the same applies to headSet() too, its return type is SortedSet, so it can return any implementor class of SortedSet(including TreeSet)
thus explicit casting is required?

no. if the type [left hand side ] is SortedSet or any of its Super type. if the method[say actually returns A which implements I] returns Interface , then now you are assigning this method to the implementor class directly like below


got it?

hth

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we return a interface?