I have a very simple question.
Can 2 classes share the same Super class instance? I know the answer is no, because super is the instance itself, but I was really there was some workaround...
public class Parent{
private final int parentId;
private static final HashMap<Integer,Parent> parentMap = new HashMap<Integer,Parent>();
private Parent(int i){
parentId = i;
parentMap.put(i,this);
}
public static Parent newInstance(int i)
{
if(parentMap.containsKey(i))
return parentMap.get(i);
return new Parent(i);
}
}
/* Other class */
public class ExtendedParent extends Parent{
public ExtendedParent(int i){
super(i);//I should use the factory at this point...
}
public static main(String[] args){
/*What I am trying to achieve*/
Parent p1 = new ExtendedParent(1);
Parent p2 = new ExtendedParent(1);
if(p1.equals(p2))
System.out.println("This is what i aim to get!!!!");
}
}
Remade the code to demonstrate my problem clearly.
Can someone help me out? =D
Thanks in advance!
I see two alternatives:
Make ExtendedParent
instances forward calls to a Parent
instance they keep as a member. And methods that should not only forward the calls, add the additional processing that distinguishes ExtendedParent
from Parent
.
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