user2583693
user2583693

Reputation: 1

Type casting between two different classes

I have a class Propositions which is an array list of class

Proposition: I want to make a tree, whose Nodes are from class ConstituentSet or Proposition. In the Tree just leaves are from class Proposition and all the internal Nodes are from class ConstituentSet.

I do not know how should I define the type of children in class ConstituentSet. If I define from type ConstituentSet, I can not set my leafs in this Type(because they are from Proposition) and if I set the children from type Proposition, I cannot set my internal nodes.

public class  ConstituentSet<T> {       
    protected ConstituentSet<T> child1, child2;    
    //OR       
    protected Proposition child1,child2;
}

public class Proposition { 
    private  Property property;
    private Rating       rating;  
    private Type         type;  
    private Serializable value;
}

Upvotes: 0

Views: 128

Answers (2)

Joris W
Joris W

Reputation: 517

implement an interface

public interface TreeElement(){
     // define methods
}

and implement both Proposition as ConstituentSet with this interface

public class constituentSet implements TreeElement{
     protected ArrayList<TreeElement> children;
     // rest of code here
}

public class Proposition implements TreeElement{
    // code here
}

Upvotes: 0

Juvanis
Juvanis

Reputation: 25950

Let your Propositions and ConstituentSet implement a common interface, and then constitute a tree from the instances of this interface.

Upvotes: 6

Related Questions