This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Subclass calling style?

 
Ranch Hand
Posts: 45
8
  • Mark post as helpful
  • send pies
  • 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: 11188
244
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Mark post as helpful
  • send pies
  • 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: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 14758
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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.
 
a wee bit from the empire
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!