• 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

passing a class to a method?

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I posted this before using an example of a zookeeper and different kinds of animals, but I think my explanation of my question was not clear enough, so I will try again.
Suppose I have this:



The methods finalAnswerB and finalAnswerC are identical except that they call a different method. And they are so long that I really do not want to have them written out twice. So here is where I can take advantage of polymorphism:


In the second example, an object is passed to the finalAnswer method. However, it seems strange to me because the answer method is a static method, and usually you do not have to make an object in order to use a static method. In this case, I really should not have to make an object, I think. If it is possible, I would rather pass the class name (B or C) to the finalAnswer method, or pass the name of a method to the finalAnswer method. Is it possible to do that in java?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should note that the version in class C will not get called since statics methods do not take part in polymorphism. Hence you do not override from B but redefine. And the method that makes the call to answer(int[] x) takes B as an argument.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If it is possible, I would rather pass the class name (B or C) to the finalAnswer method,



Depends on what you mean by this. If you mean an instance of B or C, then yes, it is possible. If you mean passing a string with "B" or "C", then yes, it is also possible.


or pass the name of a method to the finalAnswer method.



Yes, of course, you can pass a string with the name of the method. However, to call that method, you will need to use the reflection libraries.

Henry
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
Static methods do not take part in polymorphism? I didn't know that.
What is the difference between overriding and redefining?
Would this work?



What are the reflection libraries?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What are the reflection libraries?



The reflection API allows you to get / set variables, call methods, and instatiate objects -- all without knowing the name of the classes , the name of the methods, or the name of the variables, at compile time.

Henry
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic