Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors chaining from a superclass  RSS feed

 
Jean-Yves Tinevez
Greenhorn
Posts: 22
Eclipse IDE Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jean-Yves Tinevez
Greenhorn
Posts: 22
Eclipse IDE Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
Eclipse IDE Mac Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!