• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Subclass calling style?

 
Ranch Hand
Posts: 45
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any thoughts on preferences / style of calling subclass method? Given:

Do ya'll prefer

or
or something else? (Animal is from an external class library and changing it is not a feasible option.)
 
Saloon Keeper
Posts: 14488
325
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally I'd tell you to use the visitor pattern, but that requires that the Animal class has a visitBy() method, and you said you don't control it.

Honestly, it really doesn't matter. Do what feels good to you. Some days I'll assign to a separate variable first, and other days I can't be bothered.
 
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gerard Charles wrote:. . . Do ya'll prefer . . . or . . .

No. I don't prefer either. One you start using instanceof (except when overriding the equals() method), I start getting suspicious about your use of inheritance. What you are doing is adding a method which isn't in the superclass; I think that is called functional extension as opposed to type extension, but I am not sure. It means your Dog class has a bark() method which isn't in the superclass. Can you hold you hand on your heart and say both of the following?

1: A dog is an animal.
2: A Dog object is an Animal object.

Does your Animal have a makeNoise() method or similar? Can you override it like this?Or like this?If you really can't get away without writing a bark() method, I would probably write the second form, but you might be able to use javap −c Dog to view the bytecode, and find that there isn't any difference betwen the two forms after they have been compiled.
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the developers of Java seem to have had some strong opinions on this matter, since they produced a proposal named JEP 305: Pattern Matching for instanceof. And if I read that proposal correctly it's already released in Java 14.
 
Sheriff
Posts: 17279
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gerard Charles wrote:Any thoughts on preferences / style of calling subclass method? Given:

Do ya'll prefer ... or ...


Neither. Both usages fly in the face of the principles of polymorphism. You'll have to come up with a more compelling example for using instanceof.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic