II.B.Tech, II- Sem. Objective MID TERM-I Exam Branch: EEE ...
Exam Objective : Legal return types
description
Transcript of Exam Objective : Legal return types
![Page 1: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/1.jpg)
Exam Objective : Legal return Exam Objective : Legal return typestypes
PRESENTED BY :
SRINIVAS VG
![Page 2: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/2.jpg)
Agenda :Agenda :
Return types on overloaded methods
Overriding and Return types, and Covariant returns
Returning a value
![Page 3: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/3.jpg)
Return types on Overloaded Return types on Overloaded methods :methods : To overload a method , you
must change the argument listEg:- public class Foo{
void go(){} }
public class Bar extends Foo{String go(int x){return null;
} }
![Page 4: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/4.jpg)
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
![Page 5: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/5.jpg)
Return types while Overriding :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 returnclass Alpha {
Alpha dostuff(char c){return new Alpha();
}}
class Beta extends Alpha {Beta doStuff(char c){ // legal override in java 1.5return new Beta();
}}
![Page 6: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/6.jpg)
Covariant returns :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
![Page 7: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/7.jpg)
Till now we know :Till now we know :
Overloaded methods can
change the return types
Overridden methods cannot, except in the case of covariant returns
![Page 8: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/8.jpg)
Returning a value : There are 6 Returning a value : There are 6 rulesrules You can return null with an object
reference return typeEg: 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); }}
![Page 9: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/9.jpg)
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]);
}}
![Page 10: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/10.jpg)
You can return any value or variable that can be implicitly converted to the declared return typeEg : public int foo(){
char c='c';return c; // char is compatible with int}
Eg: class Dummy{ public int dum(){ char c='a'; return c; } public static void main(String args[]){ Dummy n=new Dummy(); int x=n.dum(); System.out.println("x value is "+x ); } }
Valid only for primitive return types :
![Page 11: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/11.jpg)
You can return any value or variable that can explicitly cast to declared return typeEg : 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 ); }}
![Page 12: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/12.jpg)
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
![Page 13: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/13.jpg)
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(){return new Horse(); //Assume Horse extends Animal}
Eg: 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();
}}
![Page 14: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/14.jpg)
More Examples :More Examples :Eg (1) : public Object getObject() { int[] nums = {1,2,3}; return nums; // Return an int array,
// which is still an object }
![Page 15: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/15.jpg)
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(); } }
![Page 16: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/16.jpg)
Eg (3) : public abstract class Animal { } public class Bear extends Animal { } public class Test { public Animal go() { return new Bear(); // OK, Bear "is-a" Animal
}
}
- This code will compile, the return value is a subtype
![Page 17: Exam Objective : Legal return types](https://reader036.fdocuments.in/reader036/viewer/2022062520/568159f9550346895dc74596/html5/thumbnails/17.jpg)