Can anyone tell me whether the return type of the method will also be considered when you are overriding a method? what i mean exactly is whether the return type can be different when you are overriding a method.and somewhere i read like overloading will not come under polymorphism. is this true?
overriding and overloading in java
Collapse
X
-
Tags: None
-
Originally posted by ramadeviirrigir eddyCan anyone tell me whether the return type of the method will also be considered when you are overriding a method? what i mean exactly is whether the return type can be different when you are overriding a method.and somewhere i read like overloading will not come under polymorphism. is this true? -
Originally posted by ramadeviirrigir eddyCan anyone tell me whether the return type of the method will also be considered when you are overriding a method? what i mean exactly is whether the return type can be different when you are overriding a method.and somewhere i read like overloading will not come under polymorphism. is this true?
[CODE=java]
public class Overriding {
public void myFunction()
{
System.out.prin tln("I'm in the void.");
}
public int myFunction()
{
System.out.prin tln("I'm in the int.");
return 0;
}
}
[/CODE]It will tell you
Code:Duplicate method myFunction() in type Overriding
[CODE=java]
public class Overriding {
public static void main(String[] args)
{
myFunction();
int i = (Integer) myFunction(3);
}
// This could be a void without the return line
public static Object myFunction()
{
System.out.prin tln("I'm in the void.");
return new Object();
}
// This could be "public static int myFunction(int i)" too
public static Object myFunction(int i)
{
System.out.prin tln("I'm in the int.");
return i;
}
}
[/CODE]works fine, while it won't without the argument in the second function. What you can do is have a function, which takes and returns an int, one that takes and returns a double, etc. and call them the same. As long as the arguments are different, it'll work.
Example:
[CODE=java]
public int myFunction(int i)
{
System.out.prin tln("I'm in the int.");
return i;
}
public double myFunction(doub le d)
{
System.out.prin tln("I'm in the double.");
return d;
}
[/CODE]
Greetings,
Nepomuk
*Edit*
Of course, when using Generics, you can do some wonderful things. Read about them. However, that's not exactly what you're looking for, if I understood correctly. The compiler or jvm doesn't find out the type itself.Comment
-
Hi,
The code you mentioned above is an example of overloading right. it is not overriding.i mean to say this code.
[CODE=java]
public int myFunction(int i)
{
System.out.prin tln("I'm in the int.");
return i;
}
public double myFunction(doub le d)
{
System.out.prin tln("I'm in the double.");
return d;
}
[/CODE]
but overriding means you should have same method signature like the following code
[CODE=java]
class One
{
int a=10,b=20;
public int add()
{
System.out.prin tln("a+b is:" + (a+b));
return a+b;
}
}
class Two extends One
{
int a=20,b=20;
public int add()
{
System.out.prin tln("a+b is:" + (a+b));
return a+b;
}
}
public class Three {
/**
* @param args
*/
public static void main(String[] args) {
Two x = new Two();
x.add();
One y = new One();
y.add();
}
}
[/CODE]
If you mention the return type as different in the above method then it'll give compilation error saying that "add() in Two cannot override add() in One; attempting to use incompatible return type" if u mention the return type in Two as double.Comment
-
Originally posted by nepomukJust try something like this:
[CODE=java]
public class Overriding {
public void myFunction()
{
System.out.prin tln("I'm in the void.");
}
public int myFunction()
{
System.out.prin tln("I'm in the int.");
return 0;
}
}
[/CODE]It will tell you
Code:Duplicate method myFunction() in type Overriding
public class Overriding {
public static void main(String[] args)
{
myFunction();
int i = (Integer) myFunction(3);
}
// This could be a void without the return line
public static Object myFunction()
{
System.out.prin tln("I'm in the void.");
return new Object();
}
// This could be "public static int myFunction(int i)" too
public static Object myFunction(int i)
{
System.out.prin tln("I'm in the int.");
return i;
}
}
[/CODE][CODE=java]
public int myFunction(int i)
{
System.out.prin tln("I'm in the int.");
return i;
}
public double myFunction(doub le d)
{
System.out.prin tln("I'm in the double.");
return d;
}
[/CODE]
Greetings,
Nepomuk
I think you are talking about overloading here not overriding.Comment
-
Yes i too think that you are talking about overloading.
Code:class One { int a=10,b=20; public int add() { System.out.println("a+b is:" + (a+b)); return a+b; } } class Two extends One { int a=20,b=20; public double add() { System.out.println("a+b is:" + (a+b)); return a+b; } } public class Three { /** * @param args */ public static void main(String[] args) { Two x = new Two(); x.add(); One y = new One(); y.add(); } }
if you compile this program it'll give error saying that "add() in Two cannot override add() in One; attempting to use incompatible return type"
by this example can i say that "overriding method should have same return type also including the method signature."???Comment
-
Overloaded methods need to have different parameter lists (types and or number)
while for overridden methods the parameter lists need to be assignable and the
return type of the overridden method must be a supertype or identical to the
return type of the overriding method; here's a small example:
[code=java]
import java.util.Colle ction;
import java.util.List;
class Base {
public Collection func(int i) { return null; }
public Object func(String s) { return null; }
}
public class Derived extends Base {
public List func(int i) { return null; }
public String func(String s) { return null; }
}[/code]
kind regards,
JosComment
-
Originally posted by JosAHOverloaded methods need to have different parameter lists (types and or number)
while for overridden methods the parameter lists need to be assignable and the
return type of the overridden method must be a supertype or identical to the
return type of the overriding method; here's a small example:
[code=java]
import java.util.Colle ction;
import java.util.List;
class Base {
public Collection func(int i) { return null; }
public Object func(String s) { return null; }
}
public class Derived extends Base {
public List func(int i) { return null; }
public String func(String s) { return null; }
}[/code]
kind regards,
JosComment
-
[CODE=java]class Foo {
public Number print() {
System.out.prin tln("Foo");
return 0;
}
}
class Bar extends Foo {
public Integer print() {
System.out.prin t("Bar");
return 0;
}
public static void main(String[] args) {
Foo foo = new Foo();
Bar bar = new Bar();
foo.print();
bar.print();
}
}[/CODE]
Y A T F E
Edit: and Jos beat me to the punch againComment
-
Originally posted by ramadeviirrigir eddyok thanks a lot. now i understood the difference clearly.
So, thanks to ramadeviirrigir eddy, madhoriya22 and JosAH for pointing out my mistake. ^^
Greetings,
NepomukComment
-
Originally posted by r035198x[CODE=java]class Foo {
public Number print() {
System.out.prin tln("Foo");
return 0;
}
}
class Bar extends Foo {
public Integer print() {
System.out.prin t("Bar");
return 0;
}
public static void main(String[] args) {
Foo foo = new Foo();
Bar bar = new Bar();
foo.print();
bar.print();
}
}[/CODE]
Y A T F E
Edit: and Jos beat me to the punch again
I run ur example and got these two compile time errors ..
Code:Foo.java:8: The method java.lang.Integer print() declared in class Bar cannot override the method of the same signature declared in class Foo. They must have the same return type. public Integer print() { ^ Foo.java:4: Incompatible type for return. Can't convert int to java.lang.Number. return 0; ^ 2 errors
Confused !Comment
-
Originally posted by madhoriya22Hi,
I run ur example and got these two compile time errors ..
Code:Foo.java:8: The method java.lang.Integer print() declared in class Bar cannot override the method of the same signature declared in class Foo. They must have the same return type. public Integer print() { ^ Foo.java:4: Incompatible type for return. Can't convert int to java.lang.Number. return 0; ^ 2 errors
Confused !Comment
-
Originally posted by r035198xWhat's your compiler version?
My compiler version is 1.5.0_04.
Note : I am not trying to prove anything wrong. Just trying to resolve my confusion :(Comment
Comment