1 / 17

Exam Objective : Legal return types

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

cecily
Télécharger la présentation

Exam Objective : Legal return types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG

  2. Agenda : • Return types on overloaded methods • Overriding and Return types, and Covariant returns • Returning a value

  3. Return types on Overloaded methods : • To overload a method , you must change the argument list Eg:- publicclass Foo{ void go(){} } publicclass Bar extends Foo{ String go(int x){ returnnull; } }

  4. As long as there is change in argument list , return type doesn’t have to match with that of superclass version • Find the error – publicclass Foo{ void go(){} } publicclass Bar extends Foo{ String go(){ returnnull; } } • Can’t change only the return type

  5. Return types while Overriding : • Only in JAVA 5 you’re allowed to change the return type while overriding only in case of covariant returns Eg : Look at the covariant return class Alpha { Alpha dostuff(char c){ returnnew Alpha(); } } class Beta extends Alpha { Beta doStuff(char c){ // legal override in java 1.5 returnnew Beta(); } }

  6. Covariant returns : • A class could not override the return type of the methods it inherits from a superclass • Applicable only for JAVA 5 version • A method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass Alpha Beta

  7. Till now we know : • Overloaded methods can change the return types • Overridden methods cannot, except in the case of covariant returns

  8. Returning a value : There are 6 rules • You can return null with an object reference return type Eg:public Button dostuff(){ returnnull; } Eg; class Dummy{ public Dummy dum(){ returnnull; } publicstaticvoid main(String args[]){ Dummy n=new Dummy(); Dummy h=n.dum(); System.out.println(h); } }

  9. An array can be returned Eg: public String[] go(){ returnnew String[]{"Fred","Barney","Wilma"}; } Eg: class Dummy{ public String[] dum(){ returnnew String[] {"fred","Barney","wilma"}; } publicstaticvoid main(String args[]){ Dummy n=new Dummy(); String str[]=n.dum(); System.out.println(str[0]+" "+str[1]+" "+str[2]); } }

  10. Valid only for primitive return types : • You can return any value or variable that can be implicitly converted to the declared return type Eg : publicint foo(){ char c='c'; return c; // char is compatible with int } • Eg: class Dummy{ • publicint dum(){ • char c='a'; • return c; • } • publicstaticvoid main(String args[]){ • Dummy n=new Dummy(); • int x=n.dum(); • System.out.println("x value is "+x ); • } • }

  11. You can return any value or variable that can explicitly cast to declared return type Eg : publicint foo(){ float f=32.5f; return (int)f; } Eg: class Dummy{ publicint dum(){ float f=32.5f; return (int)f; } publicstaticvoid main(String args[]){ Dummy n=new Dummy(); int x=n.dum(); System.out.println("x value is "+x ); } }

  12. If the declared return type is void, then you should not return anything Eg : publicvoid 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

  13. With an object reference return type , you can return any object type that can implicitly cast to the declared return type Eg : public Animal getAnimal(){ returnnew Horse(); //Assume Horse extends Animal } Eg: class Animal{ } class Horse extends Animal{ public Animal getAnimal(){ System.out.println("I am inside "); returnnew Horse(); } publicstaticvoid main(String args[]){ Horse h=new Horse(); Animal animals=h.getAnimal(); } }

  14. More Examples : • Eg (1) : public Object getObject() { • int[] nums = {1,2,3}; • return nums; // Return an int array, • // which is still an object • }

  15. Eg (2) : • interface Chewable{ • void hello(); • } • class NotGum { • publicvoid bye(){ • System.out.println(" bye "); • } • } • class Gum implements Chewable{ • publicvoid hello(){ • System.out.println(" hello "); • } • } • publicclass TestChewable{ • public Chewable getChewable(){ • returnnew Gum(); • } • publicstaticvoid main(String args[]){ • TestChewable tt=new TestChewable(); • Chewable ch; • ch=tt.getChewable(); • } • }

  16. Eg (3) : • publicabstractclass Animal { } • publicclass Bear extends Animal { } • publicclass Test { • public Animal go() { • returnnew Bear(); // OK, Bear "is-a" Animal • } • } • - This code will compile, the return value is a subtype

  17. Thank You

More Related