I am learning Java, and found a code segment as follows.

public static void displayClusters(final Collection<Cluster> clusters)
    {
        displayClusters(clusters, Integer.MAX_VALUE);
    }

    public static void displayClusters(final Collection<Cluster> clusters,
        int maxNumberOfDocumentsToShow)
    {
        displayClusters(clusters, maxNumberOfDocumentsToShow,
            ClusterDetailsFormatter.INSTANCE);
    }

    
    public static void displayClusters(final Collection<Cluster> clusters,
        int maxNumberOfDocumentsToShow, ClusterDetailsFormatter clusterDetailsFormatter)
    {
        System.out.println("\n\nCreated " + clusters.size() + " clusters\n");
        int clusterNumber = 1;
        for (final Cluster cluster : clusters)
        {
            displayCluster(0, "" + clusterNumber++, cluster, maxNumberOfDocumentsToShow,
                clusterDetailsFormatter);
        }
    }

It seems to me that there are three different implementations for displayClusters with different argument settings.

I am interested in knowing the point of this kind of design. And how the process is going when we have the following invocation in terms of the above three different implementations.

// Show clusters
        if (clusters != null)
        {
            displayClusters(clusters);
        }

Recommended Answers

All 7 Replies

Research method overloading.

Basically it allows a coder to use the same method name for different but very related cases.

Thanks for the response.

With respect to this call of

displayClusters(clusters);

, it will invoke those three different implementations sequentially due to its particular parameter setting. Is my understanding correct?

Research method overloading.

Basically it allows a coder to use the same method name for different but very related cases.

invoke those three different implementations sequentially

The call you posted matches only one method. That is the one that will be called.

The called method can then make any calls its wants.

Yes, the first two are adding default parameters and forwarding to the next more complex method signature.

Member Avatar for hfx642

I have several DEBUG routines that I call for... well... debugging.
I may want to display; an integer, a double, a string, an array of integers, an array of doubles, an array of strings.
I have (in this case) six different DEBUG routines defined.
I just call DEBUG (x). One call for all of my debugging.
Java will figure out which one it will execute.
This is called OverLoading.
It comes in VERY handy!

it is method overloading FYI it is allowed to write many functions with the same name and return type .

but different parameters , one important note this parameters mustn't accept any type of cast .

i.e. if you have the following method definitions :

int x (int p);
int x (double p);
if you called x(5); which one will be called ?? you guess this will give you compilation error .

but if you write the following :
nt x (int p);
int x (String p); the compiler have no problem to work .

i hope i helped .

one important note this parameters mustn't accept any type of cast .
i.e. if you have the following method definitions
int x (int p);
int x (double p);
if you called x(5); which one will be called ?? you guess this will give you compilation error .

public class Demo {
   
   static void x(int p)    {System.out.print("X"+p);}
   static void x(double p) {System.out.println(", Y"+p);}
   
   public static void main(String[] args) {
    x(5);
    x(5d);
   }
   
}

... gives the output X5, Y5.0, no compilation or runtime errors.

It also works if the parameters are both integer types

public class Demo {
   
   static void x(int p)  {System.out.print("X"+p);}
   static void x(long p) {System.out.println(", Y"+p);}
   
   public static void main(String[] args) {
    x(5);
    x(5l); // that's a lower case L, not a digit 
   }
   
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.