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
What is string literal in java?
What is a lock or purpose of locks in java?
What is the difference between inheritance and encapsulation?
What is a byte string?
Why does java not allow multiple public classes in a java file ?
What are the two basic ways in which classes that can be run as threads may be defined?
Is nan false?
What is the difference between Error, defect,fault, failure and mistake?
Can we create an object if a class doesn't have any constructor ( not even the default provided by constructor ) ?
Explain public static void main(string args[]) in java.
Is void a data type in java?
What are synchronized methods ?
How to do a true java ping from windows?
How to implement an arraylist in java?
How do you use parseint in java?