• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Inheritance and constants

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While developing an app some time ago, I had an interesting (in my opinion) but wrong solution to a problem I had.
Basically I was implementing 3 kinds of table models which where similar, but had different numbers of columns, the app would request the needed table model from a factory class, which would intanciate a needed table model based on the parameter I would send to it. All 3 table models would inherit from one class (which extended TableModel).
Now, the solution I came up with on paper was to declare an abstract static int in the parent class, and a function getColumnCount would return it. The constant itself would be declared in the inheriting classes (I use it in other functions as well). This way I wouldn't have to have getColumnCount function in every extending class. below is something that it could have looked like:

As you know Java does not allow abstract fields.
Is there a way to implement something like this is Java, without actually having to retype the getColumnCount() function everywhere?
And does anyone knows the OO reason why the static fields can not be abstract?
[ January 19, 2004: Message edited by: Yuriy Grechukhin ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is that only methods are polymorphic, not fields. So the solution is to have an abstract getter method.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course there is another solution, which might better fit your needs:
 
Yuriy Grechukhin
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Of course there is another solution, which might better fit your needs:


Thanks a lot, I like you solution! I'll try to think about it when I'll have a similar problem in the future.
 
I think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic