A
A
Artem Matyashov2016-11-23 20:29:49
Java
Artem Matyashov, 2016-11-23 20:29:49

How to pass an object of an unknown type to a method?

We have an interface:

public interface Sorter {
    void sort(Integer[] array);
}

And several classes that implement this interface:
public class BubbleSort implements Sorter{...}
public class MergeSort implements Sorter{...}
public class QuickSort implements Sorter {...}
public class ShellSort implements Sorter{...}

I want to write a class for testing the speed of algorithms, describe a method, say testSortMethod , that would take any of the objects of the class described above and call the sort method on them . It is advisable to do this in a loop, that is, put all the objects in an array and pass them one by one to the testSortMethod method . Is it possible to do this and how?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question