Friday, January 18, 2008

ClassLoaders and class visibility

Assume you have one interface and one implemenation class

/* package */ interface com.abc.Xyz{

...

}

public class com.abc.XyzImpl implements Xyz {

...

}

Now if you keep Xyz in one classpath and XYZImpl in another classpath which is a child of former one you will get the following exception.

"java.lang.IllegalAccessError: class com.abc.XyzImpl cannot access its superinterface com.abc.Xyz" This is because eventhough both are in package, they are loaded by different classloaders and hence in different name space. If you define the package also public no issue else both class have to be loaded by the same classloader.

2 comments:

Matt said...

May I ask what exactly you mean by "define the package also public"? Make the interface public? As far as I know any Java interface is inherently 'public', even if you don't specify a modifier.

Anonymous said...

how can you keep one class in one classpath and another in different classpath.
Please elaborate?