java - Inheritance with Generics -
i trying implement recursive tree structure arbitrary keys in java. want have tree<x,y>
holds x
, more (sub)trees, indexed set of y
s. however, think since trees used indexing data in readonly disk file, tree should read-only. so, in order create them, made subclass, mutabletree
, should allow editing operations on tree
.
here code:
public class tree<c,k> implements serializable { protected c content; protected java.util.hashmap<k, tree<c,k>> nexts; protected tree () {} public c getcontent() { return content; } public java.util.iterator<k> getkeys () { return nexts.keyset().iterator(); } public tree<c,k> descend(k key) { return nexts.get(key); } }
and mutabletree
:
public class mutabletree<c,k> extends tree<c,k> { public mutabletree (tree<c,k> par) { super(); this.content = par.content; this.nexts = par.nexts; } public mutabletree () { super(); } public void setcontent (c c) { this.content = c; } public mutabletree<c,k> addkey (k k) { mutabletree<c,k> noo = new mutabletree<c,k>(); nexts.put(k, noo); return noo; } public boolean delkey (k k) { return (nexts.remove(k)!=null)?true:false; }
}
this snippet not compile, opting instead complain tree.content
, tree.nexts
protected. can see, indeed are. however, mutabletree
subclass of tree
, shouldn't have access parent's protected fields?
thanks help.
you can access protected
members through references of same type code, or subtype.
just in case, because creating mutabletree
allow client code mutate supposedly immutable tree
.
Comments
Post a Comment