| Author |
Constructors chaining from a superclass
|
Jean-Yves Tinevez
Greenhorn
Joined: Feb 12, 2007
Posts: 22
|
|
Hi everyone! It's my first ime here so System.out.println("hello world!"). I guess it's not the right topic to talk about my life, but here is the context of my silly question. I want to develop a numerical simulation program for biophysics, which i take as an opportunity to learn Java. One of the feature of this program is to allow to load a set of parameters, which are xml files, and to display them in a JTree. This is done. This work. I was wondering how i could link the item in the jtree to the corresponding JDom Document. I thought of simply use a subclass of DefaultMutableTreeNode: i extend this class in a TreeParametersNode subclass, which is only different from its superclass by the fact that it has an additionnal field, the reference to the JDom document. I coded this, which was simple: extends DefaultMutableTreeNode then add a setter and a getter to the subclass for the JDom Document. The problem is the constructor of this class. I didn't find a way to properly inherit all the constructors from the superclass. I would like the subclass to have exactly the same constructors has the superclass (i.e. DefaultMutableTreeNode) without having to reenter them in the subclass source. Anyone has an idea of how this can be achieved, giving the fact that DefaultMutableTreeNode has pleny of constructor for every kind of object given by the argument of its constructors? Thanks in advance!
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
Welcome to JavaRanch! Constructors are not inherited the same way that methods are inherited. If you want your subclass to have the same constructors as the superclass, then you have no other choice than to implement all the constructors in the subclass.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Jean-Yves Tinevez
Greenhorn
Joined: Feb 12, 2007
Posts: 22
|
|
Ouille ! This is going to take long... Does anybody knows a simple way to add a field to an existing object or class? Anyway, thanks for your answer, which will save me from looking or asking for something that does not exist. jy
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
A long time to implement a load of constructors? No. Put the superclass on an IDE eg Eclipse, then new->class->extends SuperClass, and tick the box with "implement superclass constructors" on. It only takes a few seconds. I don't usually add fields like that, but I am pretty sure the IDEs will do that too. CR
|
 |
Jean-Yves Tinevez
Greenhorn
Joined: Feb 12, 2007
Posts: 22
|
|
Hi! Well i did it another way: i put in each DefaultMutableTreeNode an object that embed everything i need to display the JDom docuement properly. Thanks! jy
|
 |
 |
|
|
subject: Constructors chaining from a superclass
|
|
|