Thanks again for your follow up. Here is what im saying:
//**** File 1 *******************************************
package Package1;
class BaseClass
{
void someBaseClassFunction() {}
int someBaseClassData;
}
interface Interface
{
void interfaceFunction();
}
public class Package1PublicClass extends BaseClass implements Interface
{
public static void main(
String []args)
{
System.out.println("Package1PublicClass");
}
public void interfaceFunction()
{
System.out.println("Package1PublicClass.interfaceFunction()");
}
}
//**** File 2 ************************************************
package Package2;
import Package1.*;
public class Package2PublicClass
{
public static void main(String []args)
{
System.out.println("Package2PublicClass");
Package1PublicClass c = new Package1PublicClass();
c.interfaceFunction();
//^ I don't want to allow this, but have no way
// to prevent it based on public access modifer
// in the interface
// c.someBaseClassFunction();
//^This is a compilation error, which is what i want
}
}
//*********************************************************
What I want is to be able to treat classes that implement Package1.Interface polymorphically within Package1. That is, classes within Package1 can call the function interfaceFunction() on classes that implement Package1.Interface. However, I don't want classes outside Package1 to be able to call the function interfaceFunction().
If the Java language was written such that members of interfaces were given package access by default, then the function Package1PublicClass.interfaceFunction() could also have package access, and classes outside of the Package would not be able to call interfaceFunction() in classes that implemented that interface.
However, it appears that you have no choice in the matter because Java defines that all members of an interface be public. You cannot make its access more restrictive in the implementing class and therefore limit the accessibility of these functions to only package access.
So, you are stuck with not being allowed to restrict the accessibility of functions that are "inherited" from interfaces. However, there seems to be no such restiction for functions that are "inherited" via the extends clause. The line c.someBaseClassFunction(); wont compile because the base class defined someBaseClassFunction() to have package access, and the derived class also defines someBaseClassFunction() to also have package access, so I cannot call it from Package2, which is what I want.
So, I understand that there is this restiction in Java, but I dont understand why the language was defined this way.
It seems that by doing this (making members of interfaces public), they:
1) Broke the consistency of the language w.r.t. modifiers. In all other places, the absense of a modifier implies package access. For interfaces, it implies public.
2) Makes it impossible to achive the kind of access restriction that is possible with classes that I have shown above.
So, I see two negatives as a result of this decision in the language, but no positives. What am i missing?