3

Below interface define in default package

public interface Foo{  
}  

package com.code  
public class  MyClass implements Foo{  
}

Above code will give following compilation error:
Foo can not be resolved to type
why???

user1866527
  • 31
  • 1
  • 2
  • Possible duplicate of [Is use of Java's default package bad style?](http://stackoverflow.com/questions/7849421/is-use-of-javas-default-package-bad-style) – trashgod Nov 30 '12 at 14:34
  • See http://stackoverflow.com/questions/283816/how-to-access-java-classes-in-the-default-package – blank Nov 30 '12 at 14:34

2 Answers2

1

That is why it is recommended that you put all your code into packages.

When you reference a class or interface without using a package name then the assumption is that the class is in the same package as the code in which it is referenced. So the compiler is seeing this:

  package com.code  
  public class  MyClass implements com.code.Foo{  
   }

Since there is no way to reference the default package in code then do not use it.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
0

If you want your class to implement an interface,then you should either have both of them in the same package Or import the package which contains the interface before creating your class Or use the whole path of the interface in the declaration.

Kash
  • 43
  • 1
  • 11