This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing a class to a method?

 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • 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?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic