What is the process of defining a method in a sub class having same name & type signature as a method in its superclass?

Method overloading[edit | edit source]

In a class, there can be several methods with the same name. However they must have a different signature. The signature of a method is comprised of its name, its parameter types and the order of its parameters. The signature of a method is not comprised of its return type nor its visibility nor the exceptions it may throw. 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.

Nội dung chính

  • Method overloading[edit | edit source]
  • Variable Argument[edit | edit source]
  • Constructor overloading[edit | edit source]
  • Method overriding[edit | edit source]
  • What is the process of defining two or more methods within same class that have same name but different parameters declaration a method overloading?
  • What is the process of defining two or more methods within same class that have same name but different parameters declaration in Java?
  • Is process that can create multiple methods of the same name in the same class?
  • What is the process of defining more than one method in a class having same name but different signature?

Methods with the same name in a class are called overloaded methods. Overloading methods offers no specific benefit to the JVM but it is useful to the programmer to have several methods do the same things but with different parameters. For example, we may have the operation runAroundThe represented as two methods with the same name, but different input parameter types:

Code section 4.22: Method overloading.

public void runAroundThe[Building block] { ... } public void runAroundThe[Park park] { ... }

One type can be the subclass of the other:

Although both methods would be fit to call the method with the String parameter, it is the method with the nearest type that will be called instead. To be more accurate, it will call the method whose parameter type is a subclass of the parameter type of the other method. So, aObject will output Object. Beware! The parameter type is defined by the declared type of an object, not its instantiated type!

The following two method definitions are valid

Code section 4.23: Method overloading with the type order.

public void logIt[String param, Error err] { ... } public void logIt[Error err, String param] { ... }

because the type order is different. If both input parameters were type String, that would be a problem since the compiler would not be able to distinguish between the two:

Code section 4.24: Bad method overloading.

public void logIt[String param, String err] { ... } public void logIt[String err, String param] { ... }

The compiler would give an error for the following method definitions as well:

Code section 4.25: Another bad method overloading.

public void logIt[String param] { ... } public String logIt[String param] { String retValue; ... return retValue; }

Note, the return type is not part of the unique signature. Why not? The reason is that a method can be called without assigning its return value to a variable. This feature came from C and C++. So for the call:

Code section 4.26: Ambiguous method call.

the compiler would not know which method to call. It is also the case for the thrown exceptions.

Test your knowledge

Question 4.6: Which methods of the Question6 class will cause compile errors?

Question6.java

public class Question6 { public void example1[] { } public int example1[] { } public void example2[int x] { } public void example2[int y] { } private void example3[] { } public void example3[] { } public String example4[int x] { return null; } public String example4[] { return null; } }

Answer

Question6.java

public class Question6 { public void example1[] { } public int example1[] { } public void example2[int x] { } public void example2[int y] { } private void example3[] { } public void example3[] { } public String example4[int x] { return null; } public String example4[] { return null; } }

The example1, example2 and example3 methods will cause compile errors. The example1 methods cannot co-exist because they have the same signature [remember, return type is not part of the signature]. The example2 methods cannot co-exist because the names of the parameters are not part of the signature. The example3 methods cannot co-exist because the visibility of the methods are not part of the signature. The example4 methods can co-exist, because they have different method signatures.

Variable Argument[edit | edit source]

Instead of overloading, you can use a dynamic number of arguments. After the last parameter, you can pass optional unlimited parameters of the same type. These parameters are defined by adding a last parameter and adding ... after its type. The dynamic arguments will be received as an array:

Code section 4.27: Variable argument.

public void registerPersonInAgenda[String firstName, String lastName, Date... meeting] { String[] person = {firstName, lastName}; lastPosition = lastPosition + 1; contactArray[lastPosition] = person; if [meeting.length > 0] { Date[] temporaryMeetings = new Date[registeredMeetings.length + meeting.length]; for [i = 0; i

Chủ Đề