I am new to Java and am struggling with associations. I have a class
public class A{
private string name;
public string get_name() {
return name;
}
public void set_name(string name) {
name = name;
}
}
I have an association with another class B as association A -> B. It is one to one association. Now what I would like to do is to create another class C which is of type class A. So in other words, C is the same class as A but with different name. But there is 1 to many association between A and C. I would appreciate any help!
If
CisAwith a different name why not just initialize an instance ofAand name itC?Alternatively, if
Cis going to change over time but be reliant onAmakeCextendAHowever, I would be wary of using the above as its generally a bad idea to make a superclass concrete (What if A changes down the line and you don’t want C to inherit those changes?). So, make
Aan interface/abstract class and make bothCandAconcreteimplement it.Something like the following would be good…