What is it called when a class can have more than one method with the same name if their arguments are different?

In the code below, two methods are named by the same name, what are the consequences?

And another question, how can I reduce the amount of code?

public String getFileName(Uri uri) {
    String fileName = null;
    String scheme = uri.getScheme();

    if (scheme == null)
        return null;

    if (scheme.equals("file")) {
        return uri.getLastPathSegment();
    } else if (scheme.equals("content")) {
        Cursor cursor = mContext.getContentResolver().query(uri, null, null, null, null);

        if (cursor != null) {
            if (cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
                cursor.moveToFirst();
                fileName = cursor.getString(columnIndex);
            }
            cursor.close();
        }
    }

    return fileName;
}

public static String getFileName(String path) {
    if (path == null)
        return null;

    int index = path.lastIndexOf(PATH_SEPERATOR);
    return index < path.length() ? path.substring(index + 1) : null;
}

asked Mar 10, 2020 at 15:07

What is it called when a class can have more than one method with the same name if their arguments are different?

2

You are talking about methods, right?
So you are using method overloading here. There are no consequences, if only you do not get confused with a large number of overloaded methods.

Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading, that allows a class to have more than one constructor having different argument lists.

answered Mar 10, 2020 at 15:13

What is it called when a class can have more than one method with the same name if their arguments are different?

1

As Vitaly explained, you're overloading a method there. That's fine, because the signature of the two methods is different.
A method's signature is define by its name and its parameter types.

In your example, you have

getFileName(Uri)

and

getFileName(String)

So, the compiler can tell, based on the parameter you provide, which is the method you actually want to invoke.

answered Mar 10, 2020 at 17:20

jmmjmm

1,0241 gold badge12 silver badges37 bronze badges

Introduction

Overriding and overloading are the core concepts in Java programming. They are the ways to implement polymorphism in our Java programs. Polymorphism is one of the OOPS Concepts.

What is it called when a class can have more than one method with the same name if their arguments are different?
Screenshot of Java code with arrows pointing at instances where overloading and overriding are occurring.

When the method signature (name and parameters) are the same in the superclass and the child class, it’s called overriding. When two or more methods in the same class have the same name but different parameters, it’s called overloading.

Comparing overriding and overloading

OverridingOverloading
Implements “runtime polymorphism” Implements “compile time polymorphism”
The method call is determined at runtime based on the object type The method call is determined at compile time
Occurs between superclass and subclass Occurs between the methods in the same class
Have the same signature (name and method arguments) Have the same name, but the parameters are different
On error, the effect will be visible at runtime On error, it can be caught at compile time

Overriding and overloading example

Here is an example of overloading and overriding in a Java program:

package com.journaldev.examples;

import java.util.Arrays;

public class Processor {

	public void process(int i, int j) {
		System.out.printf("Processing two integers:%d, %d", i, j);
	}

	public void process(int[] ints) {
		System.out.println("Adding integer array:" + Arrays.toString(ints));
	}

	public void process(Object[] objs) {
		System.out.println("Adding integer array:" + Arrays.toString(objs));
	}
}

class MathProcessor extends Processor {

	@Override
	public void process(int i, int j) {
		System.out.println("Sum of integers is " + (i + j));
	}

	@Override
	public void process(int[] ints) {
		int sum = 0;
		for (int i : ints) {
			sum += i;
		}
		System.out.println("Sum of integer array elements is " + sum);
	}

}

Overriding

The process() method and int i, int j parameters in Processor are overridden in the child class MathProcessor. Line 7 and line 23:

public class Processor {

    public void process(int i, int j) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {
 
    @Override
    public void process(int i, int j) {  /* ... */ }

}

And process() method and int[] ints in Processor are also overridden in the child class. Line 11 and line 28:

public class Processor {

    public void process(int[] ints) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {

    @Override
    public void process(Object[] objs) { /* ... */ }

}

Overloading

The process() method is overloaded in the Processor class. Lines 7, 11, and 15:

public class Processor {

    public void process(int i, int j) { /* ... */ }

    public void process(int[] ints) { /* ... */ }

    public void process(Object[] objs) { /* ... */ }

}

Conclusion

In this article, we covered overriding and overloading in Java. Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.

Can a class have more than one method with the same name?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.

What is it called when more than one method presents in the same class with the same name and different argument?

The practice of defining two or more methods within the same class that share the same name but have different parameters is called overloading methods.

What is it called when you have multiple methods of the same name but have different numbers or types of parameters?

Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.

Is the class having methods that are the same name with different arguments?

If a class has multiple methods having same name but parameters of the method should be different is known as Method Overloading.