Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing instances of a particular class to be created by only one other class in Java?

Suppose that I have two classes A and B and I want to make it so that instances of B can only be created in A and in B itself. I don't want any other class (including subclasses of A) to be allowed to create instances of B. Is there any way of doing this in Java?

Here is a bit of code if it's not clear what I am trying to do:

public class A {
    B instance;
    public A(){
        // Still allows for subclasses to access B
        instance = B.getInstance((Object)this);
    }
}

Here is the class whose construction I want to limit:

public class B {

    // If I make this public all classes can create it, but
    // if I make it private without any getter methods then
    // no other classes but itself can create it
    private B(){}

    // Problem with this is that subclasses of  A
    // can also create instances of B
    public static B getInstance(Object o){
        if(o instanceof A)
            return new B();
        else
            return null;
    } 
}

I have tried Googling and searching on StackOverflow for possible solutions, but the closest thing that I have found is using a Singleton design pattern with a modified getInstance() method to make sure only a class with a particular type can have access to instances of class B. While this works fairly well, it still enables any subclass which extends A to get instances of B. Is there any way to stop this from occurring or would it ruin the whole point of subclassing if a subclass couldn't do what its superclass could do?

like image 991
DiscoChinchilla Avatar asked Nov 16 '13 01:11

DiscoChinchilla


People also ask

Can a Java class contain an instance of another class?

Yes, that is of course possible.

Can you create an instance of a class within the same class?

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. To instantiate an inner class, you must first instantiate the outer class.

What is a particular instance of a class in Java?

An instance of a class is an object. It is also known as a class object or class instance. As such, instantiation may be referred to as construction. Whenever values vary from one object to another, they are called instance variables. These variables are specific to a particular instance.

Which class can only have one instance?

A Singleton class in Java allows only one instance to be created and provides global access to all other classes through this single object or instance.


3 Answers

Suppose that I have two classes A and B and I want to make it so that instances of B can only be created in A and in B itself. I don't want any other class (including subclasses of A) to be allowed to create instances of B.

You could make B a private inner class of the A class.

like image 50
Hovercraft Full Of Eels Avatar answered Nov 14 '22 23:11

Hovercraft Full Of Eels


While this works fairly well, it still enables any subclass which extends A to get instances of B. Is there any way to stop this from occurring or would it ruin the whole point of subclassing if a subclass couldn't do what its superclass could do?

If you don't want your class A to be subclassed, you can make class A final, or can have a private constructor for A. Although as suggested in previous answers,, its better to create private inner classes.

public final class A {
    B instance;
    public A(){
        // Still allows for subclasses to access B
        instance = B.getInstance((Object)this);
    }
}
like image 37
Nitin Dandriyal Avatar answered Nov 14 '22 22:11

Nitin Dandriyal


A can present something to B that only A can possess. For example

public class A
{
    public static class Pass
    {
        private Pass(){}

Only A can create an A.Pass object. If such objects are only transmitted from A to B, nobody else can get a hold of them and pretend to be A.

public class B
{
    public static B getInstance(A.Pass token)
    {
        if(pass==null) 
            throw ...      
        else
            caller must be A
like image 34
ZhongYu Avatar answered Nov 14 '22 23:11

ZhongYu