• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Child class using methods of parent

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My GUI started out with an interface that used a class SeriesPlot to produce a plot in a JPanel



this was used to make more specific panels





Worked like a charm. but now there is a new requirement- a popup on the chart that writes out a file with a combination of temperature or humidity specific information and the data values (requirements seem to be written in parallel with code for this project- drives me nuts)

I started out writing methods writeDataset for HumidityPlot and TemperaturePlot thinking I could do something like:



Of course that gives:

cannot find symbol
symbol : method writeCoordinateData(java.io.PrintWriter)
location: class java.awt.Container
thisPanel.getParent().writeDataset(outFile);
^
1 error

But I cannot just cast to a JPanel, because that does not have a writeDataset method either.

So I was thinking I would need to write an interface with that method and modify my classes as follows:




and in SeriesPlot



I hesitate to try the simpler solution of passing in the humidity and temperature specific data, as its likely to be of different types and format and would need to be updated anytime the parent changed. I don't want temperature and humidity specific code in SeriesPlot, as it is likely to be reused for other plot panels. I'd thought about getting the parent object and using instanceof as well, but then SeriesPlot would need to be edited for each new class.

Am I going about this the right way or am I just fixated on my particular solution?

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first thing I wonder is why TemperaturePlot and HumidityPlot don't extend SeriesPlot. Wasn't the idea that these add specificity to a generalized class? Does SeriesPlot provide common behavior that any kind of plot could take advantage of?

The second thing I wonder is whether, perhaps, you might be mixing up the parent/child relationship of an inheritance tree with the parent/child relationship among GUI components? Keep in mind that though we use the same terminology they're very different things. When you call getParent() on a GUI component, you're going to get whatever GUI component it's contained in. That could be a [_]Plot, but it could be something else couldn't it?.

Jon Swanson wrote:I don't want temperature and humidity specific code in SeriesPlot, as it is likely to be reused for other plot panels.


Quite right. If TemperaturePlot and HumidityPlot did extend SeriesPlot, then they could override its methods where necessary, supplying the appropriate temperature- or humidity-specific behavior. If there is enough in common among the various different kinds of plots, then it makes sense for them to extend a common superclass.

Your interface idea isn't bad. You can also use interfaces and inheritance together to strengthen your design and more clearly express its intent. You don't have to choose one over the other.
 
Jon Swanson
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dennis,

Maybe you can help me get my terminology a little straighter. I THINK I was intending to use composition rather than inheritance. The only person with Java experience I know locally has a strong bias for the former over the latter.

If I was using inheritance, then SeriesPlot (which is a JPanel that has a plot) could be extended to make HumiditySeriesPlot and I could override the writeDataset method so that it looked for a parent of class HumidityPlot. HumidityPlot would create a JPanel of class HumiditySeriesPlot. Likewise, I could extend SeriesPlot to make TemperatureSeriesPlot.

I believe that I was using composition to construct HumidityPlot. It is also a JPanel that contains three other JPanels, one of which is an instance of SeriesPlot. It gets information from the other panels to tell SeriesPlot precisely what to plot. I was thinking the other way around, allowing the SeriesPlot class to make use of either the HumidityPlot or TemperaturePlot's writeDataset method by defining an interface for SeriesPlot to use.

So using the terminology of inheritance, HumiditySeriesPlot is a child of SeriesPlot, and can use its methods. But SeriesPlot is not a child of HumidityPlot, so cannot use its methods (except indirectly by having a pointer to the HumidityPlot instance).

The way I had things arranged, for SeriesPlot, which is one of three panels in HumidityPlot, to use a function in HumidityPlot (the function pulls stuff from the other panels), was to use the getParent() method because SeriesPlot is a panel added to the HumidityPlot panel, but not because of an inheritance relationship.

The complication in my case, is the popup is not from HumidityPlot, which would simplify things a lot. Instead, the request was to add an additional item to SeriesPlot's existing popup, but which needs information unknown to SeriesPlot, unlike the any of the existing items on that popup which can be handled by SeriesPlot alone. But I don't write the requirements, so I have to make it work.

Have I said this all correctly?
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic