8 Replies Latest reply: Dec 3, 2012 11:00 PM by 977534 RSS

    Implemenet/Extend interface/class in default package

    977534
      Filename: DeaultInterface.java
      
      public interface DefaultInterface {
          // Abstract Methods...
      }
      Filename: ConcreteClass.java
      
      package com.company;
      
      import DefaultInterface;
      
      public class ConcreteClass implements DefaultInterface {
          // Implementation of Abstract Methods...
      }
      When I compile above java code it gives error "cannot find symbol. symbol: class DefaultInterface".

      Can anyone explain why can't we implement the interface/extend the class in default package(no package)?

      Edited by: 974531 on Dec 2, 2012 11:07 PM
        • 1. Re: Implemenet/Extend interface/class in default package
          Kayaman
          974531 wrote:
          Can anyone explain why can't we implement the interface/extend the class in default package(no package)?
          Does it really matter?

          You can do it if your class is in the default package too, but it's just smarter not to use the default package.
          • 2. Re: Implemenet/Extend interface/class in default package
            977534
            Yeps, it's good practice to use packages. But why it is not allowed to (implement interface)/(extend the class) which is in default package into other package?
            • 3. Re: Implemenet/Extend interface/class in default package
              EJP
              Because it was made illegal in Java about 9 years ago in response to some otherwise unresolvable ambiguities.
              • 4. Re: Implemenet/Extend interface/class in default package
                977534
                Thanks. I understand now. It will create an ambiguity if interface/class with the same name exists in the other package also.
                • 5. Re: Implemenet/Extend interface/class in default package
                  rp0428
                  >
                  When I compile above java code it gives error "cannot find symbol. symbol: class DefaultInterface".

                  Can anyone explain why can't we implement the interface/extend the class in default package(no package)?
                  >
                  You CAN implement the interface defined in the default package.

                  What you CANNOT do is import it. And that is because the Java Language Specification specifies that named types can only be referenced by their simple name if they are imported and must otherwise be referenced by their fully qualified name.

                  See the 7.5 Import Declarations in the Java Language Specification - http://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html
                  >
                  An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.

                  Without the use of an appropriate import declaration, the only way to refer to a type declared in another package, or a static member of another type, is to use a fully qualified name (§6.7).
                  . . .
                  A type in an unnamed package (§7.4.2) has no canonical name, so the requirement for a canonical name in every kind of import declaration implies that (a) types in an unnamed package cannot be imported, and (b) static members of types in an unnamed package cannot be imported. As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time error on any attempt to import a type (or static member thereof) in an unnamed package.
                  >
                  So you can't reference that type by importing it because you can't import a type that doesn't have a canonical nmae. And you can't just use the simple name because, as the first statement above says you have to import it to use the simple name.

                  When you use the simple name of that type without an import the simple name would refer to a class IN your 'com.company' named package.
                  • 6. Re: Implemenet/Extend interface/class in default package
                    977534
                    thanks for more specific answer.

                    Edited by: keyur259 on Dec 3, 2012 11:23 PM
                    • 7. Re: Implemenet/Extend interface/class in default package
                      EJP
                      Thanks. I understand now. It will create an ambiguity if interface/class with the same name exists in the other package also.
                      That's not true, and it isn't what I said.
                      • 8. Re: Implemenet/Extend interface/class in default package
                        977534
                        Didn't you mean the ambiguity created for DefaultInterface in ConcreteClass by below file structure?
                        src
                        │   DefaultInterface.java
                        │
                        └───com
                            └───company
                                    ConcreteClass.java
                                    DefaultInterface.java