Restrict Access of method calling in Java

Viewed 2893

I have a class A. This class contains two methods let say method1() and method2(). Now I have two other classes named B and C. Both classed will contain the same instance of A class.

Now I want to restrict access in such a way that my 'Bclass can only callmethod1()and my otherC' class can call method2(). The design scheme is as follow-

class A {
    void method1(){/*-------------*/}
    void method2(){/*-------------*/}
}

now if I create an instance of A and share it with my B and C class

class B {
    A ob;
    public B(A ob) {
        this.ob=ob;
    }
    public void process() {
        ob.method1(); //only call method1()
        ob.method2(); //can't access it.
    }
}

class C {
    A ob;
    public C(A ob) {
        this.ob=ob;
    }
    public void process() {
        ob.method2(); //only call method2()
        ob.method1(); //can't access it.
    }
}
5 Answers
Related