Saturday, April 14, 2012

Does return type matters in case or overriding ???? Yes OR No?

Ques: Does return type matters in case or overriding ???? Yes OR No?
Ans: It matters ,but not in case of Inheritance

/**
* The Class NatureOfReturnTypeWhileOverriding.
*
* @author Abhinav kr.mishra
*/
public class NatureOfReturnTypeWhileOverriding {

/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {

Super superObj=new Super();
superObj.noReturn();
superObj.returnInt(12);
superObj.returnInteger();
superObj.returnNumber();
superObj.returnObject();
Sub subObj=new Sub();
subObj.noReturn();
subObj.returnInt(12);
subObj.returnInteger();
subObj.returnNumber();
subObj.returnObject();

}

}

class Super{

public Super() {
super();
System.out.println("Super instantiated..");
}

public void noReturn(){
System.out.println("super.noReturn");
}

public int returnInt(int x){
System.out.println("super.returnInt(..) : "+x);
return x;
}

public Integer returnInteger(){
System.out.println("super.returnInteger(..)");
return new Integer(1);
}

public Number returnNumber(){
System.out.println("super.returnNumber(..)");
return new Integer(1);
}

public Super returnObject(){
System.out.println("super.returnObject");
return new Super();
}

}

class Sub extends Super{

public Sub() {
super();
System.out.println("Sub instantiated..");
}


//this method is of no return type so we can not return any thing from this method.
public void noReturn(){
System.out.println("sub.noReturn");
}


/*
* This is also allowed,because primitive types can be returned as per Boxing rule provided my Java 5
*/
public Integer returnInteger(){
System.out.println("sub.returnInteger(..)");
return 1;
}

public int returnInt(int x){
System.out.println("sub.returnInt(..) : "+x);
return x;
}

/* This is not allowed.even 'int' and 'char' both are primitive type.
* public char returnInt(int x){
System.out.println("sub.returnInt(..)");
return x;
}*/

/*
* Return types during overriding does not matters only in this case,
* i.e when return type is subclass or superclass
*
*/
public Sub returnObject(){
System.out.println("sub.returnObject>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.");
return new Sub();
}

/*
*This is allowed because Integer is the Subclass of Number.
*
*/
public Integer returnNumber(){
System.out.println("sub.returnNumber(..)>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.");
return new Integer(1);

}

}

OUTPUT>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Super instantiated..
super.noReturn
super.returnInt(..) : 12
super.returnInteger(..)
super.returnNumber(..)
super.returnObject
Super instantiated..
Super instantiated..
Sub instantiated..
sub.noReturn
sub.returnInt(..) : 12
sub.returnInteger(..)
sub.returnNumber(..)>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.
sub.returnObject>> RETURN TYPE DOES NOT MATTERS IF INHERITENCE.
Super instantiated..
Sub instantiated..
***************************************************************************

Conclusion: "So we can say return type does not matters when it is in inheritance relation"

No comments:

Post a Comment

Thanks for your comments/Suggestions.