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

What are internal and external variables?

546


How we can make copy of a java object?

653


What is nullpointerexception in java?

525


Which number is denoted by leading 0x or 0x in java?

564


Explain the importance of throws keyword in java?

569






What is use of valueof () in java?

564


Name the components that are termed to be Heavy-weight component but available in Light-weight components?

1987


Explain hashset and its features?

583


Which arithmetic operations can result in the throwing of an arithmeticexception?

599


What is thread start?

522


What modifiers may be used with a top-level class?

578


What is difference between next () and nextline () in java?

540


what is encapsulation in java? Explain

664


Which package is always imported by default?

543


What is :: operator in java?

511