• 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

Unknown Subclasses

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've got a class called Field and it's got several subclasses such as TextField and LabelField.

I'm working with a method that returns the Field that currently has focus:

Field field = screen.getFieldWithFocus();

I don't know the type it will return, so I do something like this:

if(field instanceof TextField){ TextField tf = (TextField)field }
if(field instanceof LabelField){ LabelField lf = (LabelField)field }
// etc

This works, but I'm curious if there is another way of determining what the subclass is without using instanceof? Perhaps reflection?

I want it to autocast for me where I call getFieldWithFocus()

Thanks,

Drew
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it: if you were able to determine the name of an unknown subclass at runtime, what would you do with it? You can't declare a variable of this type, because variables and their types are compile-time things.

Your question makes me wonder whether you fully grok polymorphism. Can you tell me why you're interested in switching based on the type?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as the method you are calling is one that each subclass overrides from the parent class, it is not necessary to cast the reference to the subclass type. Polymorphism will bind the method call to the correct implementation based on the runtime type of the object even though the object is referred to as a Field.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
As long as the method you are calling is one that each subclass overrides from the parent class, it is not necessary to cast the reference to the subclass type.


That's the problem - the subclasses have different methods that I need to call which were not in the parent.

So the method I used is the most appropriate?

Drew
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One fix would be to include abstract methods in the superclass which match the methods you are going to have in your subclasses. In each subclass override the abstract methods but only give a default implementation to the one you don't need. That why you can call the methods using a superclass reference. Of course depending on your situation this might not be efficient.
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic