Java abstract classes/interfaces and specifying type parameters

Question!

Right now I'm implementing a Tree<T> (<T> refers to the data value of the root node) abstract class and currently have something like this:

public abstract class Tree<T> {
    public void setChild(int index, ??? childTree);
    public void deleteChild(int index, ??? childTree);
    public T getData();
    public void setData(T value);
}

As you could see, I'm having trouble figuring out the correct type for the child trees. Ideally, I'd do something like this

public abstract class Tree<T> {
    public void setChild(int index, ImplementingClass childTree);
    public void deleteChild(int index, ImplementingClass childTree);
    ...
}  

But I'm not sure how to do this (probably going to reread the docs). The current solution I have in mind is something like this

public abstract class Tree<T, C extends Tree> {
    public void setChild(int index, C childTree);
    public void deleteChild(int index, C childTree);
    ...
}

But a more correct solution would be to specify that a child tree has to be the same class as the current tree. Is there a way to specify this in Java?

By : Rdesmond


Answers
Tree<T>

will contains object of type T

Tree<String> tree;

T could be another generic type description

Tree<Tree<String>>

Then you add or delete T objects from it

Public void setChild(int index, T child);
public void deleteChild(int index, T child);

if you want to specify T to a more restrictive type

<T extends class>
<T implements interface>
<T super class>

in the first example if you haven't method implementations, change abstract class to interface.

remember that a class can inherit from only once other but can implements multiple interface

By : Tokazio


Angular cannot find "app" in the directory path. Try adding a tilde at the beginning:

<ng-include src="'app/partials/navbar.html'" ></ng-include>

Should be:

<ng-include src="'~/app/partials/navbar.html'" ></ng-include>
By : Dave Kaye


If I am reading this correctly, Message would contain a List<MessageUser>.

If that's the case then this should get you close:

var List<Message> messages = 
    ( from m 
      in db.Messages 
      where 
          m.UserID == userid || m.MessageUsers.Any(mu => mu.UserID == userID) 
      select m).ToList();
By : Sam Axe


This video can help you solving your question :)
By: admin