aspose file tools*
The moose likes Beginning Java and the fly likes passing a class to a method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "passing a class to a method?" Watch "passing a class to a method?" New topic
Author

passing a class to a method?

Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
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

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Kevin Tysen
Ranch Hand

Joined: Oct 12, 2005
Posts: 255
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: passing a class to a method?