• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Is using java.lang.Object as a method parameter a good idea?

 
Greenhorn
Posts: 4
Mac Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a util method to traverse an object using reflection and recursion to extract important values.

public static void traverseObject(ClassA objectInstance){}

This method is being used to traverse all the class instances which extend "ClassA".
Now I want to use this same method in another component for traversing another class "ClassB".

So my question is: Is it ok to change the method parameter to java.lang.Object and put this method in a shared component. Or should this method be duplicated in both components?

I have seen people use java.lang.Object as a parameter while doing a deep copy. So is it alright to change the method to:

public static traverseObject(Object objectInstance){}

Or is there a better way to handle it?


The traversal code is pretty generic and will work with any Java Object.
I don't have any concerns that it will break if it's used with any other classes other than "ClassA" and "ClassB".
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ninad Naik wrote:
The traversal code is pretty generic and will work with any Java Object.



Then, yes, it's appropriate to accept Object as a parameter. One caveat is that if, for some reason, it only makes sense to accept certain types from a business logic perspective--even if technically it can work on any object--then you might want to declare the method to accept a common supertype of those types.
 
Ninad Naik
Greenhorn
Posts: 4
Mac Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, from a business perspective it does make sense to be accepting only these two types. That answers my question.
Thanks a bunch, Jeff !
 
Marshal
Posts: 76868
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
knowledge is the difference between drudgery and strategic action -- tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic