G
Gianni Mariani
.... warning - I'm more familiar with C++ but I'm learning Java for new
project. I'll try hard not to troll. ...
Say I have a class that exposes some interfaces. The intent is that
methods may be overridden by clients but only the class that defines the
interfaces are allowed to call them.
In C++ you would do it like so:
class X
{
public:
class Y
{
friend class X;
private:
virtual void = 0;
};
void YConsumer( Y & y )
{
y.FuncOnlyForX();
}
};
class YClient : public X::Y
{
public:
void FuncOnlyForX() // override X::Y
{
std::cout << "THIS IS YClient\n";
}
};
int main()
{
X x;
X::Y & y = YClient();
y.FuncOnlyForX(); // error
x.YConsumer( y ); // OK
}
I tried this in Java but the compiler keeps on getting in the way:
public class X
{
public interface Y
{
private void FuncOnlyForX(); // javac saya illegal use of private
}
public void YConsumer( Y y )
{
y.FuncOnlyForX();
}
}
public class Yclient extends X.Y
{
void FuncOnlyForX()
{
}
}
So, how do you get java to enforce use visibility policy on interfaces ?
project. I'll try hard not to troll. ...
Say I have a class that exposes some interfaces. The intent is that
methods may be overridden by clients but only the class that defines the
interfaces are allowed to call them.
In C++ you would do it like so:
class X
{
public:
class Y
{
friend class X;
private:
virtual void = 0;
};
void YConsumer( Y & y )
{
y.FuncOnlyForX();
}
};
class YClient : public X::Y
{
public:
void FuncOnlyForX() // override X::Y
{
std::cout << "THIS IS YClient\n";
}
};
int main()
{
X x;
X::Y & y = YClient();
y.FuncOnlyForX(); // error
x.YConsumer( y ); // OK
}
I tried this in Java but the compiler keeps on getting in the way:
public class X
{
public interface Y
{
private void FuncOnlyForX(); // javac saya illegal use of private
}
public void YConsumer( Y y )
{
y.FuncOnlyForX();
}
}
public class Yclient extends X.Y
{
void FuncOnlyForX()
{
}
}
So, how do you get java to enforce use visibility policy on interfaces ?