Type Checking.
So I need to know a little bit more about type checking when it comes to Abstract Classes.
I am currently writing a program that is uses dynamic allocation. And to avoid writing a tree and list handler for each different object. I am making each different object inherit from an abstract class and I am writting a general tree and list handler using the abstract class. My question is, is there any way that I can use java to force the list to be in the specific subclass type?
For example lets say that I have 2 classes that inherit from my TreeNode Class, they are Book and Anime. When I build a tree for Book I want that tree to reject any attempts to add an anime node to it and vice versa. Does java have this feature built in?
> Does java have this feature built in?It's called generics.
> It's called generics.
Ok thanks, I am just wondering how exactly generics work when based in an abstract class? Like I said my TreeNode class is the super class for my Book class. I would just like to know a little bit more. If someone could direct me to a tutorial that would be most helpful.
> Ok thanks, I am just wondering how exactly generics
> work when based in an abstract class?
They work the same as for non-abstract classes. The two concepts are orthogonal.
> Like I said my
> TreeNode class is the super class for my Book class.
> I would just like to know a little bit more. If
> someone could direct me to a tutorial that would be
> most helpful.
Did you not like anything you got when you googled "java generics tutorial"? Hard to recommend anything in that case.
Ok so here is a question.If i have abstract class TreeNode<T> and I have Book inherit TreeNode, what becomes of the <T>? Are all the private varibles that are TreeNode<T> transformed into TreeNode<Book> or what?
> > Ok thanks, I am just wondering how exactly
> generics
> > work when based in an abstract class?
>
> They work the same as for non-abstract classes. The
> two concepts are orthogonal.
>
> > Like I said my
> > TreeNode class is the super class for my Book
> class.
> > I would just like to know a little bit more. If
> > someone could direct me to a tutorial that would
> be
> > most helpful.
>
> Did you not like anything you got when you googled
> "java generics tutorial"? Hard to recommend anything
> in that case.
I did Google it and I got nothing that answers my questions about inheriting a generic abstract class.
Is the TreeNode class used in the implementation of Tree or is TreeNode objects just stored in Tree objects?
> Is the TreeNode class used in the implementation of> Tree or is TreeNode objects just stored in Tree> objects?A TreeNode is exactly what its name suggests a node in a tree.
> A TreeNode is exactly what its name suggests a node> in a tree.So Book is a special kind of TreeNode just like the name suggests?I didn't expect this kind of attitude from someone who's asking for help. Good luck.
> > Is the TreeNode class used in the implementation
> of
> > Tree or is TreeNode objects just stored in Tree
> > objects?
>
> A TreeNode is exactly what its name suggests a node
> in a tree.
This is not a very good answer to a question which is in fact essential. You didn't realize that because you haven't studied the tutorial. So you are in no position to judge that a helping hand's question is not worth answering properly and directly. You should consider that other people may have good reasons for asking what they ask, when they are trying to help you.
Anyway.
Case 1: Tree does not call methods specific to the types it can store.
You can make a generic Tree<T> and instantiate it as Tree<Book> or Tree<Anime> and you have no need to have a common supertype TreeNode just to have a type be storable in your tree.
Case 2: Tree does call methods specific to the types it can store (e.g. nodes have to be notified of insertion, removal of children...)
You have to make a common supertype TreeNode. You make a generic Tree<T extends TreeNode> and equally instantiate it as Tree<Book> or Tree<Anime>.
This is not the end of it since your Book and Anime should probably not be TreeNodes themselves so you would have a TreeNode<T>, a Tree<T extends TreeNode><T>> and BookNode extends TreeNode<BookNode> which wraps a Book containing the non-tree specific functionality of a Book. There, if this doesn't convince you you should really study the tutorial and play around a bit yourself... ;-)
Message was edited by:
Loko
Lokoa at 2007-7-8 21:47:54 >

> Ok so here is a question.
>
> If i have abstract class TreeNode<T> and I have Book
> inherit TreeNode, what becomes of the <T>? Are all
> the private varibles that are TreeNode<T> transformed
> into TreeNode<Book> or what?
It defaults to <Object> if I'm not mistaken, if you don't get a compiler error. But this has nothing to do with whether the class being inherited from is abstract or not.
And inheritance doesn't change any private variables, I don't understand that part of the question.
> I did Google it and I got nothing that answers my
> questions about inheriting a generic abstract class.
Well, based on the times of the posts here you spent less than an hour reading the tutorial. It's not that simple and based on the other posts I don't think you understand the concept properly. I think you should spend more time reading the tutorial so you get the general idea before jumping in and trying to apply it to your particular problem.