1 2 Previous Next 25 Replies Latest reply: Jul 13, 2005 12:03 AM by 807603 Go to original post RSS
      • 15. Re: Can an Interface Extend a class
        807603
        I would think that putting a class inside an interface would be similar to using an interface to define constants. It can be done, but that doesn't mean that it should be done. I haven't actually tried it (as I indicated I might do above).
        • 16. Re: Can an Interface Extend a class
          807603
          One good example of a class in an interface is a null object pattern implementation. I suppose it could be debated about where the null object should exist, but the closer to the interface, the better, IMHO.
          interface SomeContainer {
            int getCount();
            void add(SomeThing s);
          
            public static final SomeContainer EMPTY = new SomeContainer() {
              public int getCount() {
                return 0;
              }
              public void add(SomeThing s) {
                throw new UnsupportedOperationException();
              }
            };
          }
          Though this example uses an anonymous inner class, the same can be said about abstract implementation support classes. The closer they are located to the interface, the better. This yields smaller seeming packages without a bunch of abstract clutter classes.
          • 17. Re: Can an Interface Extend a class
            807603
            How interface Servlet extends class Object?
            abstract public interface Javax.servlet.Servlet extends java.lang.Object
            • 18. Re: Can an Interface Extend a class
              807603
              > How interface Servlet extends class Object?
              abstract public interface Javax.servlet.Servlet
              extends java.lang.Object

              That's all kinds of wrong.

              ~
              • 19. Re: Can an Interface Extend a class
                807603
                MrManoj writes "extends", but probably means "has superclass" here, as all valid interfaces must have (and do have) superclass java.lang.Object. So one could say that in a sense, interfaces implicitly "extend" a class. For instance you can't compile
                interface WontWork {
                  void wait();  // final in Object
                }
                • 20. Re: Can an Interface Extend a class
                  807603
                  > all valid interfaces must have (and do have) superclass java.lang.Object.

                  No. An interface can only have superinterfaces, not a superclass. If an interface does not have a superinterface (and there is "no single interface of which all interfaces are extensions"), the interface implicitly declares public abstract methods that correspond to each public method in java.lang.Object.

                  See Chapter 9 of the Java Language Specification.

                  http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html

                  > So one could say that in a sense, interfaces implicitly "extend" a class.

                  I wouldn't say that, for reasons listed above.

                  > For instance you can't compile...

                  That's specified by the JLS: "It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object."

                  ~
                  • 21. Re: Can an Interface Extend a class
                    807603
                    > all valid interfaces must have (and do have)
                    superclass java.lang.Object.


                    No. An interface can only have superinterfaces, not a
                    superclass. If an interface does not have a
                    superinterface (and there is "no single interface of
                    which all interfaces are extensions"), the interface
                    implicitly declares public abstract methods that
                    correspond to each public method in
                    java.lang.Object.
                    >
                    Ahhh. I often wondered about that.
                    • 22. Re: Can an Interface Extend a class
                      807603
                      And just to emphasize:

                      "While every class is an extension of class Object, there is no single interface of which all interfaces are extensions."
                      -- JLS, �9.1.3

                      ~
                      • 23. Re: Can an Interface Extend a class
                        807603
                        cotton.m wrote:
                        Ahhh. I often wondered about that.
                        Me too.
                        "While every class is an extension of class Object, there is no single interface of
                        which all interfaces are extensions." -- JLS, �9.1.3
                        From what I understand, that does not prohibit that interfaces have a superclass. What I was referring to was the JVMS here: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#74353 , where it says (for the super_class item):

                        If the value of the super_class item is zero, then this class file must represent the class Object, the only class or interface without a direct superclass.

                        For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object.


                        yawmark wrote:
                        So one could say that in a sense, interfaces implicitly "extend" a class.
                        I wouldn't say that, for reasons listed above.
                        I was trying to look at it from MrManoj's point of view. But I agree that strictly speaking the term "extends" is wrong in this context.
                        • 24. Re: Can an Interface Extend a class
                          807603
                          Hi MLRon,
                          you said that interface cannot extend class but in case of interface CharSequence its overrides toString() method from object class. This happen when you extend the class (You can override method of any class when you extend that class to any other class. please check this.
                          • 25. Re: Can an Interface Extend a class
                            807603
                            frozencrew wrote:
                            Hi MLRon,
                            you said that interface cannot extend class but in case of interface CharSequence its overrides toString() method from object class. This happen when you extend the class (You can override method of any class when you extend that class to any other class. please check this.
                            So? Does it have a method body? Technically the interface only overrides the Javadoc to make it more specific.
                            1 2 Previous Next