java - public interface - private menthods
- From: Gianni Mariani <gi4nospam@xxxxxxxxxx>
- Date: Wed, 13 Feb 2008 22:16:31 GMT
.... 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 ?
.
- Follow-Ups:
- Re: java - public interface - private menthods
- From: Steven Simpson
- Re: java - public interface - private menthods
- From: Daniel Pitts
- Re: java - public interface - private menthods
- From: Joshua Cranmer
- Re: java - public interface - private menthods
- Prev by Date: Re: Memory Issue
- Next by Date: Re: java - public interface - private menthods
- Previous by thread: reverse-iterator for LinkedList
- Next by thread: Re: java - public interface - private menthods
- Index(es):
Relevant Pages
|