• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can we return a interface?

 
Prateek Rawal
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dog d = (Dog)a; here a is not a Dog. so you need explicit casting to satisfy the compiler
 
Prateek Rawal
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic