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?

Answers were Sorted based on User's Feedback



There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / shamsherabanu

It is not overloading because overloading must have different parameters, no matter what ever may be the return type is.
And for overriding inheritance should be used between 2 classes.
so it is neither overriding nor overloading.

It just show compilation error as method is already defined in
same class

Is This Answer Correct ?    12 Yes 0 No

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / ashu_deepu

it is not overloading bcos different return types does not mean overloading.
n 4 overriding inheritance should b used between 2 classes.
so it is neither overriding nor overloading.

Is This Answer Correct ?    8 Yes 0 No

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / debapriya

Niether overloading nor ovverriding ,this question has errors

Overloading---->different method
signatures(order,type,number different)
Ovveriding signature same but subclass can implement this method


So ??????????????.

Is This Answer Correct ?    9 Yes 2 No

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / 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

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / debapriya maity

See overloading has nothing to do with Covariant Return
types ,in fact overloading dosent take into consideration
the return types.

But in case of ovveriding u can provide Covariant return type

say for example

class A {
protected A getModel(){
return this;
}
}
class B extends A{

public B getModel(){

return this
}
}
and there are many other examples of
covariant return types like this.

Since B is A(IS-A RelationShip)
so the return type can be a subclas of the super class

Is This Answer Correct ?    2 Yes 0 No

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / jan

The above program excute's fine ,This is overloading
In overloading concept return type in not Mandatory.

If my answer is not correct , please let me know

Is This Answer Correct ?    0 Yes 0 No

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / ashish

it is overloading not overriding.
not overriding bcz two methods are in same class and
overriding is when one class extends another class and
overrides its methods.
how overloading?
see the Example:
class Ashu
{
int a,b;

int add(int x,int y)
{
int z=x+y;
return z;
}
void add(double x, double y)
{
double z=x+y;
System.out.println(z);
}
}
class Ashish
{
public static void main(String args[])
{
Ashu obj=new Ashu();
obj.add(10.7,20.9);
int a=obj.add(10,20);
System.out.println(a);
}
}

here return types are not same but parameters are same. now
in main pogram integer or double parameters in different
calls by the object account for method overloading.

Is This Answer Correct ?    0 Yes 1 No

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / vikneswarank

Its a overLoading .because overriding have two diff class
and return type also same

but overloading have one class,and it have same method name
and diff signature.

Is This Answer Correct ?    3 Yes 8 No

There are 2 methods in a class. Both have the same method signature except for return types. Is thi..

Answer / mohamed yehiya

If both signature are same, i.e parameters
and if the method has co-variant return type
which means return type of int in one method and
return type of long in another method.
Consider safely as the method is overloaded.

if otherwise it's neither overloaded or overrided
just an another method in the class.

Is This Answer Correct ?    0 Yes 5 No

Post New Answer

More Core Java Interview Questions

What is a "pure virtual" member function?

0 Answers   Amazon,


How many decimal digits is 64 bit?

0 Answers  


Can we force garbage collector to run ?

0 Answers   B-Ways TecnoSoft,


What is bitwise complement?

0 Answers  


What are the differences between c++ and java?

0 Answers  






Can an interface extend a class?

0 Answers  


What is “try and catch” in java

2 Answers  


what is collatration?

0 Answers  


heavy components means what?

4 Answers  


What will happen if we declare don’t declare the main as static?

1 Answers  


What are the actions that can occur when a thread enters blocked state?

0 Answers  


What are packages and name a few?

4 Answers  


Categories