There are 2 methods in a class. Both have the same method
signature except for return types. Is this overloading or
overriding or what is it?

Answer Posted / s.ramesh

public class sample
{
public void displayValue (int a, int b)
{
System.out.println("a = "+a +" b = "+b);
}

public int displayValue (int a, int b)
{
System.out.println("a+b:"+(a+b));
return (a+b);
}

public static void main(String[] args)
{
sample t = new sample();
t.displayValue(10,10);
int x = t.displayValue(20,30);
}
}



Output:

D:\Prg>javac sample.java
sample.java:8: displayValue(int,int) is already defined in
sample
public int displayValue (int a, int b)
^
sample.java:18: incompatible types
found : void
required: int
int x = t.displayValue(20,30);
^
2 errors

Is This Answer Correct ?    7 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can constructor be inherited?

802


Write a java program to find the route that connects between Red and Green Cells. General Rules for traversal 1. You can traverse from one cell to another vertically, horizontally or diagonally. 2. You cannot traverse through Black cells. 3. There should be only one Red and Green cell and at least one of each should be present. Otherwise the array is invalid. 4. You cannot revisit a cell that you have already traversed. 5. The maze need not be in the same as given in the above example

2336


In a program, initializing an array of 100 KB is throwing an out of memory exception while there is 100 MB of memory available. Why?

782


How do you square a number?

883


What is hashmap in java?

819


Why local variables are stored in stack?

716


describe method overloading

727


What is the meaning of immutable regarding string?

749


What is entry set in java?

746


How arrays are stored in memory in java?

722


Can a java program have 2 main methods?

792


What is the super void?

692


How to sort numbers in java without array?

745


What is the default value of float and double datatype in java?

775


What is the purpose of a parameter?

787