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?
Yes, that is of course possible.
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.
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.
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.
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.
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With