1 2 Previous Next 15 Replies Latest reply: Jul 31, 2007 9:32 AM by 3004 RSS

    return type is an interface?

    807600
      I'm under the impression that interfaces are abstract types that cannot be instantiated. Instead when a class implements an interface, it's really just an agreement that it will provide all the methods contained within the interface as it applies to the class.

      If this is the case then how can a method have a return type of interface? Wouldn't anything that accepts that return value be instantiating the interface?

      An example of this is the collection interfaces iterator method.

      Can somebody please set me straight?

      Thanks.
        • 1. Re: return type is an interface?
          807600
          I'm under the impression that interfaces are abstract
          types that cannot be instantiated. Instead when a
          class implements an interface, it's really just an
          agreement that it will provide all the methods
          contained within the interface as it applies to the
          class.
          Exactly right
          If this is the case then how can a method have a
          return type of interface? Wouldn't anything that
          accepts that return value be instantiating the
          interface?
          No. You know how some methods have a return type ofjava.lang.Object, but can return any old object? Same thing. The return type is the most generalized type that can be returned, but any subtype of that can be returned.
          An example of this is the collection interfaces
          iterator method.
          The method will return an instance of a class - probably an inner class - that implements Iterator

          eg
          public Iterator iterator() {
             return new MyIterator();
             }
             
             // declared inside same class
             private class MyIterator implements Iterator {
              // iterator method here, as promised  by us implementing the interface
            }
          This is basic polymorphism. We use a common supertype - preferably abstract - to describe in general terms what can be done with an object - in this case, iterate over a Collection - but the exact details are defined in concrete subtypes
          • 2. Re: return type is an interface?
            807600
            Read about polymorphism. Think of interfaces as purely abstract classes. By implementing an interface, the interface becomes a supertype of the class that implements it.

            Because by definition, a class that implements an interface must provide the functionality specified by the interface, one is allowed to add another layer of abstraction.

            Ex. Many methods take "List" as a parameter, because it doesn't matter whether it is a linked or array list. The same methods are called, the actual implementation used is left to design. Very few methods specify "LinkedList" or "ArrayList" because they do not need to. By using "List", it allows for both.
            • 3. Re: return type is an interface?
              Herko_ter_Horst
              I'm under the impression that interfaces are
              abstract
              types that cannot be instantiated. Instead when a
              class implements an interface, it's really just an
              agreement that it will provide all the methods
              contained within the interface as it applies to
              the
              class.
              Exactly right
              In other words: by implementing an interface, a class agrees to act as an instance of that interface (which in Java terms it is: "myobject instanceof MyInterface" will return true if myobject is an instance of a class that implements MyInterface), and you can treat it as such.

              So even though you can't say "MyInterface myObject = new MyInterface();", you can say "MyInterface myObject = new MyClass();", when MyClass implements MyInterface.
              • 4. Re: return type is an interface?
                795672
                But if a method takes the interface type as a parameter then does this mean that using that object within the method it will only have the methods specified in the Interface, as that is all that is known to the compiler?

                Would you need to typecast the object to it's concrete type in order to utilise methods defined in the class that implements the interface?

                Sorry if the wording is confusing.
                • 5. Re: return type is an interface?
                  807600
                  But if a method takes the interface type as a
                  parameter then does this mean that using that object
                  within the method it will only have the methods
                  specified in the Interface, as that is all that is
                  known to the compiler?
                  Yes, more or less. As far as the method it's passed into is concerned, it only has those methods defined by java.lang.Object and the interface being passed in
                  Would you need to typecast the object to it's
                  concrete type in order to utilise methods defined in
                  the class that implements the interface?
                  The idea being to avoid such a situation in the first place. You would have to cast, yes, but then you're dependent on a concrete class, which is generally not advised, for this exact reason among others. If you find you're having to do that a lot, your design needs re-visiting
                  Sorry if the wording is confusing.
                  It wasn't!
                  • 6. Re: return type is an interface?
                    3004
                    But if a method takes the interface type as a
                    parameter then does this mean that using that object
                    within the method it will only have the methods
                    specified in the Interface, as that is all that is
                    known to the compiler?
                    Yes and know.

                    As far as the comiler knows, its the interface type, so you can only call the interface's methods. However, if you know what type the object actually is, you can cast the reference (as the object hasn't changed or lost anything by being referred to through the interface type).

                    However, if you're going to do that, then why did you declare the parameter to be of the interface type in the first place?


                    Would you need to typecast the object to it's
                    concrete type in order to utilise methods defined in
                    the class that implements the interface?
                    Cast the reference, not the object. But it's generally smelly if you do so.
                    • 7. Re: return type is an interface?
                      807600
                      eg
                      public Iterator iterator() {
                      return new MyIterator();
                      }
                      
                      // declared inside same class
                      private class MyIterator implements Iterator {
                      // iterator method here, as promised  by us
                      implementing the interface
                      }
                      /code]
                      
                      This is basic polymorphism. We use a common supertype
                      - preferably abstract - to describe in general terms
                      what can be done with an object - in this case,
                      iterate over a Collection - but the exact details are
                      defined in concrete subtypes
                      Thanks - but that raises a question about private classes...

                      why can a private inner class can be returned and then accessed outside of it's containing class?
                      • 8. Re: return type is an interface?
                        807600
                        eg
                        public Iterator iterator() {
                        return new MyIterator();
                        }
                        
                        // declared inside same class
                        private class MyIterator implements Iterator {
                        // iterator method here, as promised  by us
                        implementing the interface
                        }
                        /code]
                        
                        This is basic polymorphism. We use a common
                        supertype
                        - preferably abstract - to describe in general
                        terms
                        what can be done with an object - in this case,
                        iterate over a Collection - but the exact details
                        are
                        defined in concrete subtypes
                        Thanks - but that raises a question about private
                        classes...

                        why can a private inner class can be returned and
                        then accessed outside of it's containing class?
                        Because it implements that interface! Client code is not aware of the private inner class, only the interface it implements. Your code can't refer to a private inner class, but there's nothing to stop it using an instance of one, via a public interface
                        • 9. Re: return type is an interface?
                          795672
                          Thanks!

                          So just for my understanding (I know now that this is not an ideal situation), say I were to typecast the reference (something I didn't know!) to its concrete implementation how does the jvm? now know about the new methods availble to this object?

                          Are they looked up based on the objects new reference? If so where are the methods looked up from?

                          Thanks in advance.
                          • 10. Re: return type is an interface?
                            807600
                            void f(Object o) {
                                X x = (X) o;
                                x.m();
                            }
                            At compile-time, method m is verified to exist and take no args, etc..

                            At runtime, it's checked that the object o references really has type X.
                            • 11. Re: return type is an interface?
                              800322
                              So just for my understanding (I know now that this is
                              not an ideal situation), say I were to typecast the
                              reference (something I didn't know!) to its concrete
                              implementation how does the jvm? now know about the
                              new methods availble to this object?
                              Why should it need to? The JVM knows the type of the object anyway, and just goes and looks up the method in the object's class's bytecode.
                              Are they looked up based on the objects new
                              reference?
                              No. Based on the objects original type, which hasn't changed since the object was created. What you see of it for some reference during compile-time is not really relevant at run-time.
                              • 12. Re: return type is an interface?
                                795672
                                OK cheers that makes a lot of sense after your explanation and a minutes thinking.
                                • 13. Re: return type is an interface?
                                  807600
                                  The method will return an instance of a class -
                                  probably an inner class - that implements Iterator

                                  eg
                                   public Iterator iterator() {
                                      return new MyIterator();
                                   }
                                      
                                   // declared inside same class
                                      private class MyIterator implements Iterator {
                                   // iterator method here, as promised  by us
                                    implementing the interface
                                    }
                                   /code]
                                  >
                                  
                                  This is basic polymorphism. We use a common supertype
                                  - preferably abstract - to describe in general terms
                                  what can be done with an object - in this case,
                                  iterate over a Collection - but the exact details are
                                  defined in concrete subtypes
                                  I don't get it Georgemc - this is returning a new class MyIterator which implements the functions from Iterator - shouldn't it return an instance of the particular class that I'm implementing?  If I'm using a list, shouldn't the iterator function return an object of type List?  what is MyIterator?  How about an example? thouse                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                  • 14. Re: return type is an interface?
                                    3004
                                    I don't get it Georgemc - this is returning a new
                                    class MyIterator which implements the functions from
                                    Iterator - shouldn't it return an instance of the
                                    particular class that I'm implementing?
                                    It returns a MyIterator, which IS an Iterator. That's what inheritance is. If Grandchild extends Child extends Parent, then a Grandchild IS a Child and IS a Parent.

                                    If I'm using
                                    a list, shouldn't the iterator function return an
                                    object of type List?
                                    No. A List is different than a Iterator.

                                    what is MyIterator? How about
                                    an example?
                                    http://java.sun.com/docs/books/tutorial/collections/
                                    1 2 Previous Next