Hi
There is something about this code that does not seem right has anyone got any ideas? It does not matter what it does. I mean from a constructor/inheritance point of view. Some of the points I made were one constructor was passing a null value, which in turn was setting a final variable. This variable will always hold a null value. I also mentioned that ConceptA's constructor passes in a Concept instance to be set as ConceptA's parent. this did not seem correct as ConceptA extends Concept, so could pass in a ConceptB which is concrete class of Concept and set it as the parent of ConceptA, which is not good. Read the code and it will make sense.
Concept.java
public abstract class Concept {
private String id;
protected Concept(String anId) {
if (anId == null) {
throw new NullPointerException("id must not be null");
}
id = anId;
}
public String getId() {
return id;
}
public void setId(final String id) {
id = id;
}
public boolean equals(Object other) {
return other != null && other.getClass().equals(getClass())
&& id.equals(((Concept) other).id);
}
public String toString() {
return "Concept(" + id + ")";
}
}
Second class:
ConceptA.java
public class ConceptA extends Concept
{
private final Concept parent;
public ConceptA( String anId, Concept aParent )
{
super( anId );
parent = aParent;
}
public Concept getParent()
{
return parent;
}
public String toString()
{
return "ConceptA{" + getId() + ", parent=" + parent + '}';
}
}
Third class:
ConceptB.java
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
public class ConceptB extends ConceptA {
private final Set children;
public ConceptB(final String anId, final Concept aParent) {
super(anId, aParent);
children = new HashSet();
}
public int getCount() {
return children.size();
}
public void addChild(Concept aChild) {
children.add(aChild);
}
public void removeChild(Concept aChild) {
children.remove(aChild);
}
public Iterator getChildren() {
return children.iterator();
}
public int getFamilySize() {
int count = children.size();
for (Iterator iter = getChildren(); iter.hasNext();) {
count += ((ConceptB) iter.next()).getFamilySize();
}
return count;
}
public int getAncestorCount() {
int count = 0;
Concept ancestor = getParent();
while (ancestor != null) {
count++;
if (ancestor instanceof ConceptA) {
ancestor = ((ConceptA) ancestor).getParent();
} else {
ancestor = null;
}
}
return count;
}
public String toString() {
return "ConceptB{" + getId() + ", parent=" + getParent()
+ ", children=" + children.size() + "}";
}
}
Fourth class:
public class ConceptC extends ConceptA {
private static int nextSerialNo = 0;
public static int getNextSerialNo() {
return nextSerialNo++;
}
private final int serialNo;
public ConceptC(String anId) {
super(anId, null);
serialNo = getNextSerialNo();
}
public int getSerialNo() {
return serialNo;
}
public String toString() {
return "ConceptC(" + getId() + ", " + serialNo + ")";
}
}
I know this bit of problem.
public void setId(final String id) {
id = id;
}
Could u find anything else in this code?