Java: How to restrict method access to a specific class?

Here is an example:

class A 
{
   List l = new List ();
   list.insert("x");
}

class List
{
   ...
   public void insert ()
   {
      /*insertion occurs*/
   }
   ...
}

Is it possible to support the insert () method in general, but restrict access only to class A, so that no other class can access it only when called from A?

+5
source share
8 answers

If this method is publicly available, anyone can access it. An access control trick like yours is to open a set of common operations through an interface, add auxiliary operations to a private class that implements the interface, and make your users program for the interface, not for the class.

Here is an example:

public interface MyList {
    Object elementAt(int i);
}
public class A {
    private static class MyListImpl implements MyList {
        public Object elementAt(int i) {
            ...
        }
        public void insert(Object element) {
            ...
        }
    }
    private final MyListImpl list = new MyListImpl();
    public MyList getList() { return list; }
    public void insert(Object o) { list.insert(o); }
}

Usage scenario:

A a = new A();
a.insert(123);
a.insert("quick brown fox");
MyList lst = a.getList();
System.out.println(lst.elementAt(0));
System.out.println(lst.elementAt(1));
+4
source

I would pass an object that calls the method as an argument, i.e.

list.insert("x", this);

, A

 public void insert (String x, Object o)
   {
      if(o instanceof ClassA){
         /*insertion occurs*/
      }
   }
+4
package problems;

public class Problem1 {
    public static void main(String[] args) {
        B b = new B();
        b.methodInB();
        C c = new C();
        c.methodNotInB();
    }
}

class A {
    public void onlyB() {
        StackTraceElement[] stackTraceElements = Thread.currentThread()
                .getStackTrace();
        if (!problems.B.class.getCanonicalName().equals(
                stackTraceElements[stackTraceElements.length - 2]
                        .getClassName())) {
            System.err.println("You are not authorized to call me!!");
            return;
        }
        System.out.println("You are authorized to call me!!");
    }
}

class B {
    public void methodInB() {
        A a = new A();
        a.onlyB();
    }
}

class C {
    public void methodNotInB() {
        A a = new A();
        a.onlyB();
    }
}
+1

private

package com.my.stuff;

class A 
{
   List l = new List ();
   list.insert("x");

   class List
   {
      ...
      protected void insert ()
      {
      /*insertion occurs*/
      }
      ...
   }
}

, , "", ... , .

. : http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

0

, , - ( public) .

0

... insert Object , A.... , ... maby , , .

0

" " , , ( extends ). insert() protected public class A extend List. :

public class List {
   ...
   protected void insert() {
      //insertion occurs
   }
   ...
}

public class A extends List {
   ...
}

List, A List - insert().

0
source

Put it in the allowed class and make it private.

0
source

All Articles