Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG Agenda : Return types on overloaded methods Overriding and Return types, and Covariant returns Returning a value Return types on Overloaded methods : To overload a method , you must change the argument list Eg:public class Foo{ void go(){} } public class Bar extends Foo{ String go(int x){ return null; } } As long as there is change in argument list , return type doesn’t have to match with that of superclass version Find the error – public class Foo{ void go(){} } public class Bar extends Foo{ String go(){ return null; } } Can’t change only the return type Return types while Overriding : Only in JAVA 5 you’re allowed to change the return type overriding only in case of covariant returns Eg : Look at the covariant return class Alpha { Alpha dostuff(char c){ return new Alpha(); } } class Beta extends Alpha { Beta doStuff(char c){ // legal override in java 1.5 return new Beta(); } } while Covariant returns : A class could not override the return type of the methods it inherits from a superclass Applicable only for JAVA 5 version Alpha may return an object whose type is a A method in a subclass subclass of the type returned by the method with the same signature Beta in the superclass Till now we know : Overloaded methods can change the return types Overridden methods cannot, except in the case of covariant returns Returning a value : There are 6 rules You can return null with an object reference return type Eg: public Button dostuff(){ return null; } Eg; class Dummy{ public Dummy dum(){ return null; } public static void main(String args[]){ Dummy n=new Dummy(); Dummy h=n.dum(); System.out.println(h); } } An array can be returned Eg: public String[] go(){ return new String[]{"Fred","Barney","Wilma"}; } Eg: class Dummy{ public String[] dum(){ return new String[] {"fred","Barney","wilma"}; } public static void main(String args[]){ Dummy n=new Dummy(); String str[]=n.dum(); System.out.println(str[0]+" "+str[1]+" "+str[2]); } } Valid only for primitive return types : You can return any value or variable that can be implicitly converted to the declared return type Eg : public int foo(){ char c='c'; return c; // char } class Dummy{ public int dum(){ char c='a'; return c; Eg: is compatible with int } public static void main(String args[]){ Dummy n=new Dummy(); int x=n.dum(); System.out.println("x value is "+x ); } } You can return any value or variable that can explicitly cast to declared return type Eg : public int foo(){ float f=32.5f; return (int)f; } Eg: class Dummy{ public int dum(){ float f=32.5f; return (int)f; } public static void main(String args[]){ Dummy n=new Dummy(); int x=n.dum(); System.out.println("x value is "+x ); } } If the declared return type is void, then you should not return anything Eg : public void bar(){ return " jai only "; // not legal } - However for void return type you can just say as return i.e. in above just write return; // no harm in writing that With an object reference return type , you can return any object type that can implicitly cast to the declared return type Eg : Eg: public Animal getAnimal(){ return new Horse(); //Assume Horse extends Animal } class Animal{ } class Horse extends Animal{ public Animal getAnimal(){ System.out.println("I am inside "); return new Horse(); } public static void main(String args[]){ Horse h=new Horse(); Animal animals=h.getAnimal(); } } More Examples : Eg (1) : public Object getObject() { int[] nums = {1,2,3}; return nums; // Return an int array, // which is still an object } Eg (2) : interface Chewable{ void hello(); } class NotGum { public void bye(){ System.out.println(" bye "); } } class Gum implements Chewable{ public void hello(){ System.out.println(" hello "); } } public class TestChewable{ public Chewable getChewable(){ return new Gum(); } public static void main(String args[]){ TestChewable tt=new TestChewable(); Chewable ch; ch=tt.getChewable(); } } Eg (3) : public abstract class Animal { } public public public return class Bear extends Animal { } class Test { Animal go() { new Bear(); // OK, Bear "is-a" Animal } } - This code will compile, the return value is a subtype