This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
This question is based on an example of JEditorPane and JTextPane, but probably the issues are general to the class heirarchy.
OK it is my understanding that whan class B is a subclass of class A in the java class heirarchy, that means it extends that class, it adds to the functionality. One of the things that means is that from a constructor of class B you can use the super keyword to invoke a constructor of class A, thereby extending the functionality of that class A constructor.
An editor pane or a text pane can easily be loaded with text from a URL using the setPage method. The JEditorPane class also provides constructors that let you initialize an editor pane from a URL. The JTextPane class has no such constructors.
This puzzles me. In light of the fact that extending a class is supposed to expand its functionality, what is the significance of this comment which seems to suggest that in some ways a subclass has less functionality?
This seems too obvious, so no doubt it is late and I am tired and missing something. I guess not all constructors in a higher class can be invoked by super?
Indeed, but in the very same sentence on the page you linked, it says "... but the constructor of the superclass can be invoked from the subclass."
Which still leaves the question. Why would they say in the example I described that the superclass has such and such a constructor but the subclass doesn't, when ostensibly if the superclass has the constructor, the subclass doesn't need it, all the subclass should need to do is invoke said constructor using super.
Again, I guess super doesn't apply to every constructor?
I think the idea is to give you ready made constructors which you would typically use. In case you require additional functionality, you got two options. 1) Provide your own constructor and invoke the appropriate super constructor. 2) Use the required methods from the super class.
I don't think I have managed to explain things clearly.
Since this is not restricted only to Swing, I will move this to the appropriate forum. Someone else can provide a better explanation.
Joined: May 13, 2009
OK, thanks anyways. I'm beginning to see the error of my thinking. How does one invoke a JEditorPane constructor from a JTextPane class anyways. If I have a super keyword that is invoking a JEditorPane constructor, then I must be in a 'custom' subclass that extends JEditorPane, and JTextPane really doesn't enter into the picture.