-2

I am looking for a solution to a problem in Java. Hope team can help me.

Consider following classes:

Class A{
//constructor
Object obj;

public A(Object obj){
 this.obj = obj;
}

public void ma1(){
      system.out.println("In ma1");
      obj.mc1();
}

public void ma2(){
      system.out.println("In ma2");
     //A should not be able to access mc2
     obj.mc2();
}

}

Class B{

//constructor
Object obj
public B(Object obj){
 this.obj = obj;
}

public void mb1(){
system.out.println("In mb1");
// B should not be able to access mc1
obj.mc1();
}

public void mb2(){
system.out.println("In mb2");
obj.mc2();
}

}

Class C{

public void mc1(){
system.out.println("In mc1");
}

public void mc2(){
system.out.println("In mc2");
}

}

public static void main(String[] args){

A a = new A(new C);
B b = new B(new C);
a.ma1();
a.ma2();

b.mb1();
b.mb2();
}

Now I want object 'a' should be able to access mc1 and not mc2 I want object 'b' should be able to access mc2 and not mc1

Can anyone explain me how to achieve above functionality ? Note: Class A, B are my utility classes and its method are used at multiple places in my project. I want to restrict my developer from accessing particular method .

Regards, Sand

Machavity
  • 30,841
  • 27
  • 92
  • 100

1 Answers1

0

Try this code, if it match with your requirement. Create two interfaces -

public interface A1 {
    public void mc1();
}

public interface B1 {
    public void mc2();
}

public class A {
    //constructor
    A1 obj;

    public A(A1 obj){
     this.obj = obj;
    }

    public void ma1(){
          System.out.println("In ma1");
          obj.mc1();
    }

    public void ma2(){
          System.out.println("In ma2");
         //A should not be able to access mc2
         obj.mc2();
    }


}


public class B {
    //constructor
    B1 obj;
    public B(B1 obj){
     this.obj = obj;
    }

    public void mb1(){
    System.out.println("In mb1");
    // B should not be able to access mc1
    obj.mc1();
    }

    public void mb2(){
    System.out.println("In mb2");
    obj.mc2();
    }

}

implement your C class with A1 and B1 interfaces -

public class C implements A1,B1{
    public void mc1(){
        System.out.println("In mc1");
        }

    public void mc2(){
        System.out.println("In mc2");
        }
}

here is your main method -

public static void main(String[] args){

        A1 a1 = new C();
        A a = new A(a1);

        B1 b1 = new C();
        B b = new B(b1);

            a.ma1();
        a.ma2();

        b.mb1();
        b.mb2();
}
Moni
  • 433
  • 3
  • 9
  • How will these restrict A to access mc2 of class C and B from accessing mc1 of class C ? BTW thanks in advance for all the help – user3401717 Sep 25 '14 at 17:14
  • @user3401717: A only knows about A1, therefore about mc1, and not about mc2. accessing mc2 on the A1 instance will not even compile. – njzk2 Sep 25 '14 at 18:33
  • @Moni: I would recommend using the `@Override` annotation on implemented methods to help the compiler tell you about possible typos. – njzk2 Sep 25 '14 at 18:34