java - public interface - private menthods




.... 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 ?
.



Relevant Pages

  • Re: faster access private or public?
    ... > void f ... Whoops, Foo should have had: ... private, and make the original function a friend of Foo. ... Languages like Java have much more ...
    (microsoft.public.vc.language)
  • Re: JTable setValueAt problem
    ... It is worst because you have never learnt the basics of Java ... private Container con; ... public void actionPerformed{ ...
    (comp.lang.java.programmer)
  • Re: Question about Iteration and forEach
    ... I just defined a couple if interfaces; ... public static void DoTest() ... private MyListlist; ... If you want the ability to adjust your position within the enumeration, you can usually just use a normal "for" loop with an index to access individual items within the collection. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: dynamic type checking - a pauline conversion?
    ... to actually write the code in Java. ... Is the query handled ... One of the problems with Java typing is small differences in interfaces ... array iteration, List iteration, and database query result iteration all ...
    (comp.object)
  • Re: OO is not that great: many repeated codes
    ... Instances of both classes have to be able to handle the driveCarWell message - how they do handle it is implementation-dependant, and doesn't necessarily requires implementing the method in each class. ... void cookDeliciousFood() ... int average ... But it's true that Java has no support for the former and forbid the later. ...
    (comp.object)