answersLogoWhite

0


Best Answer

Multiple methods with the same name is called method overloading. The way to do this is to have the different methods accept different parameters.

Examples: Adding two values and returning the result. Let's use different methods for adding various numeric primitives.

public static int add(int a, int b) {

return a + b;

}

public static short add(short a, short b) {

return a + b;

}

public static long add(long a, long b) {

return a + b;

}

public static float add(float a, float b) {

return a + b;

}

public static double add(double a, double b) {

return a + b;

}

User Avatar

Wiki User

βˆ™ 14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 15y ago

Both True and False

Two methods can have the same name provided they have a different signature (Parameters, return type)

If they have the same signature then two methods cannot have the same name.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 11y ago

Two methods can have the same name provided their signature is different.

Ex:

public int add(int a, int b){

...

}

public int add(int a, int b, int c){

...

}

This is allowed whereas

public int add(int a, int b){

...

}

public int add(int a, int b){

...

}

This is not.

The above only applies within a single Class or Interface definition.

It is entirely possible for different classes to have a method of the exact same signature (i.e. return value, method name, argument list). If a subclass has a method with the same signature as a parent class, then that method is said to have overridden the parent class's method. There is no specific name for the case where two unrelated classes have the same method signature, though that case is perfectly legal. Different signatures with the same method name are also allowed (which is called overloading when a subclass does it).

Interfaces follow the same rules and conventions as Classes in this matter.

-------------------

Two or more methods or constructor can have the same name provided they have different signature. In other words, when we can one of those methods, we should be able to tell which method was called even when they have the same name. You can read more about overloading constructors and methods on this page: http://www.javawithus.com/tutorial/overloading-constructors-and-methods

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

You can have two methods of the same name so long as they have different signatures - this is called overloading. The methods also have to return the same type of object, and have the same access modifier. For example, you could have the following two methods in the same class: public void foo() { } public void foo(String bar) { } Both of these methods are public, and do not return an object (void). One accepts no arguments, and the other accepts an argument of type String. This can be useful if for example, if you call the method with no arguments it assumes a default value, but you call the method and pass an argument you can use the specified value instead.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

Yes they can. This is the fundamental feature of polymorphism and is called method overloading. Where you have multiple methods with the same name in a class. These methods differ from one another in the method signature alone.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

Two methods can have the same name provided their method signatures are different.

For ex:

public int add(int a, int b, int c){

}

public float add(float a, float b, float c){

}

These two methods can co-exist in the same class in spite of having the same method names because their signatures are different.

But

public int add(int a, int b, int c){

}

public int add(int a, int b, int c){

}

You cannot have this. Because it is just a replica of the earlier method and it would cause duplication.

This answer is:
User Avatar

User Avatar

Learn bay

Lvl 8
βˆ™ 2y ago

We can not define more than one method with the same name, Order, and type of the arguments. It would be a compiler error. The compiler does not consider the return type while differentiating the overloaded method.

To learn more about data science please visit- Learnbay.co

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 13y ago

False. Two methods can have the same name in Java. It is called Method Overloading.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Two methods cannot have the same name in Java A True B False?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Are true false and null Java keywords?

True and false are literals(special built-in value) in java and cannot be used as keywords.


The methods cannot have the same name in java true or false?

False. Methods in a class can have the same name as long as they have a different signature. You cannot duplicate method code inside a class but you can always have methods that have the same name but a different signature. Ex: Here I have created two methods in a class that have the same name "sum" but have a different argument types, and return types and hence perfectly allowable in a java class. Public class PolymorphismExample { public int sum(int a, int b){ return a + b; } public double sum (double a, double b){ return a + b; } }


Mixtures cannot be separated true or false?

False. Mixtures can be separated using various methods such as filtration, distillation, chromatography, and evaporation.


The name of a Java program file must match the name of the class with the extension java A True B False?

True


In Java method usually hold the properties of an object. True or False?

true


Why is the statement 'This sentence is false' so confusing?

In the logical sense, sentences must be either true or false and not both. "This sentence is false" cannot be true because that would mean that it is false, and it cannot be both. It also cannot be false because that would mean that it is true, and it cannot be both. Therefore, if it is true or false, then it is both true and false. Therefore it is either neither true nor false or both true and false; therefore, in the logical sense, it is not a sentence. However, it says it is a sentence; therefore, it is lying; therefore, it is false.


Is it true or false that scientific methods have two basic types?

true


Is it true or false that a computer cannot disobey programming instructions?

false


Is odd numbers cannot be composite true or false?

False


A theorem is proved using a geometric proof true or faulse?

Neither true nor false. Some theorems can be proven using geometric arguments and methods, others cannot.


What is literal in java programming?

Literal in java are L, F, null, true, false These act as keyword(have special meaning in java) but these does'nt comes under the category of Java Keyword.


The methods of an object are the operations that can be performed on that objects data true or false?

True.