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
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
Can we create more than one object singleton class?
What is difference between float and double?
What is a double vs float?
I have one POJO class(Java bean class), it has two variables for that it has setters and getters. Now i have created two objects for that class and i have set the data for those variables through this two objects. Now question is i want check whether those two objects have same data or not, for this write a program? Thanks, Bose.
Do we have pointers in java?
Are variables stored in ram?
What is serialVersionUID and what is its need?
What do the thread?class methods run() and start() do?
What is hypertext?
how your day start in your company
What is inner class?what is the use of inner class?where we create the object for inner class? and inner class can extend any class or inner class can implement any interface?
What is indexof?