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

TableModel abstraction?  RSS feed

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello: I have two table model objects that extend AbstractTableModel
and I want to tell the program which one to use at run time. Is it not workable to do as follows?

the error reports back that it does not know about the methods associated with the derived classes (getDefaultData, for example)

 
Jeffrey Bennett
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The myData variable is of type AbstractTableModel. While you may have instantiated it with a subclass of AbstractTableModel that has a getDefaultData() public method, the compiler only knows it as an AbstractTableModel and will limit you to the AbstractTableModel's API.

To get around this, cast it:
((SomeClass) myData).getDefaultData()

Since it looks like you are defining two classes that both have the getDefaultData() method, hopefully they either inherit from a single base-class (in which case, cast to that base class), or implement an interface defining getDefaultData() (in which case, cast to the interface).
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jeffery.

What I ended up doing after confering with some co-workers was to make an abstract class called NetTableModel that extends AbstractTableModel and abstracts all the methods that MeshDataTableModel and DefaultNetTableModel will share.

and then made each of the subclasses extend NetTableModel and implement each of the abstract methods that the two share:



now I can decide which table model will be used at run time and not have to change any other code with myData in it.
[ May 25, 2004: Message edited by: Tim McGuire ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!