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 the disadvantage of synchronization?

0 Answers  


what is business objects?

2 Answers  


Explain the use of shift operator in java. Can you give some examples?

0 Answers  


How does remove work in java?

0 Answers  


How to make a class or a bean serializable?

0 Answers  






Is .net better than java?

0 Answers  


What is re-factoring in software?

0 Answers  


Program to print 1 1 2 1 2 3 1 2 3 4 like that

8 Answers   Huawei,


What are static variables and functions?

0 Answers   Tech Mahindra,


What is nextint java?

0 Answers  


What is defined as false sharing in the context of multithreading?

0 Answers  


What do you mean by data type?

0 Answers  


Categories