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
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
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.
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
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.