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
Can array grow dynamically in java?
What is use of super keyword in java?
What are the data types supported by java? What is autoboxing and unboxing?
Can I declare a class as private?
how to create daemon thread in java?
Write a program to print fibonacci series
How to create an interface?
Where to store local variables?
Explain the significance of listiterator.
Can you access non static variable in static context?
What is anonymous inner class?
How is tree Mirroring implemented?
Why pointers are not used in java?
How does hashset work in java?
What is var keyword ?