Question

in Java, is it true that having two functions with the same name and parameters but...

in Java, is it true that having two functions with the same name and parameters but different return types will result in valid methodh overloading?

Homework Answers

Answer #1

This is not true

Explanation:

Method overloading allows a class to have more than one method with the same name but different types or different numbers of parameters.

If two methods have the same name, same number of parameters, and same type of parameters but return type is different then this is not method overloading but it will display syntax error that the method is already defined.

For example:

public class Main
{
//method to add two numbers with return type int
public static int add(int a, int b)
{
return a+b;
}
  
//method to add two numbers with return type float
public static float add(int a, int b)
{
return a+b;
}
  
   public static void main(String[] args)
   {
   //method calling and display result
       System.out.println(add(10, 20));
   }
}

Error: method add(int,int) is already defined in class.

The above program will through error because only the return type is different for the 'add' method.

So, in method overloading, the return type of the method is not included.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java programming. Fill the empty boxes with the Answer. 1) The Java compiler javac translates Java...
Java programming. Fill the empty boxes with the Answer. 1) The Java compiler javac translates Java source code into [Answer]. 2) In Java a class object can inherit the properties of a superclass by using the [Answer] keyword. 3) A(n) [Answer] method consists of the same method name but different number or types of parameters 4) In a class definition, the special method that is called to create an instance of that class is known as a/an [Answer] 5) The...
We often observe two stocks as having the same price at a particular time, but having...
We often observe two stocks as having the same price at a particular time, but having different prices before and after that time. It violates the Law of One Price. True False
1. If the technology (production function) and all the Solow model parameters are same for two...
1. If the technology (production function) and all the Solow model parameters are same for two economies, they will eventually converge to the same steady state levels of per-capita capital even if they start at different levels of initial k. True False 2. If the technology (production function) and all the Solow model parameters are same for two economies, more time taken will be needed to reach steady state for the economy with high initial level of per-capita capital? True...
In C++ ------------------------------------------ All functions take no parameters as input and return nothing. Create a function...
In C++ ------------------------------------------ All functions take no parameters as input and return nothing. Create a function for each of the following: A function that reads all text up to a space character (' ') while outputting everything up to that space A function that reads two numbers (int) ending in a ';' and adds them together and outputs the result. Note: You only have to read the first two numbers.
USING JAVA: I was asked to write a function that obtains a name from the user...
USING JAVA: I was asked to write a function that obtains a name from the user and returns it in reverse order (So if the user inputs "MIKE" the function returns "EKIM"). You can't use string variables it can only be done using a Char Array. Additionally, you can use a temporary Char Array but you are supposed to return the reverse order in the same char array that the user input, this is for hypothetical cost purposes -we are...
A. Name at least two experimental parameters that can affect the determination of the boiling point...
A. Name at least two experimental parameters that can affect the determination of the boiling point when performing a distillation. State how to optimize those two parameters so that you can get the correct boiling point. The solvent used can affect the rates of SN1 reactions. B.What common protic solvents are used for these types of reactions? Explain why the rates of SN1 reactions are faster in polar, protic solvents than in polar, aprotic solvents. C. Using structural formulas and...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and...
Using Java language.... Polymorphism of Interfaces : Take the Account class from the last chapter and make a java program for your bank. However, there is another type of customer that may not have money in the bank and may not in fact have a back account with this bank at all, but may have a loan here. Make another class in the application you are making called CarLoan. Carloan's should have a name, amount owed, a rate, and a...
Large functions aren’t very usable or maintainable. It makes a lot of sense to break down...
Large functions aren’t very usable or maintainable. It makes a lot of sense to break down the logic of a program into smaller functions that do one thing each. The program can then call these functions in sequence to perform the work. Write a program ON PYTHON that prompts for a first name, last name, employee ID, and ZIP code. Ensure that the input is valid according to these rules: • The first name must be filled in. • The...
explain why it is that two people, having chosen the same word, will likely have two...
explain why it is that two people, having chosen the same word, will likely have two different connotative meanings for the same word.
Code using Java.: Create CHECKING FUNCTIONS. boolean isGreaterThan(int[] m1, int[] m2); // Returns True if m1...
Code using Java.: Create CHECKING FUNCTIONS. boolean isGreaterThan(int[] m1, int[] m2); // Returns True if m1 > m2. boolean isEqual(int[] m1, int[] m2); // Returns True if m1 == m2.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT