Pages

Tuesday, September 27, 2011

Boxing and Unboxing in OOPS

Boxing is a process of conversion of a value to generic object type for example

Consider the following code

Int alpha=1;
object boxed_alpha= alpha;

This conversion to generic type is called as boxing

Unboxing is the reverse of a boxing continuing with the above example let us unbox boxed_alpha as a string

String string_alpha=(String) boxed_alpha;

OR

String string_alpha=boxed_alpha.ToString();

I will discuss its use subsequent comments

2 comments:

  1. Even if we convert a class type to an interface type it comes under boxing for example

    Interface vehicles
    {
    string wheeltypes();
    string steeringtype();
    }

    class car implements vehicles
    {

    public string wheeltypes()
    {

    }

    public string steeringtype()
    {

    }
    }

    public static void main()
    {
    car Maruti=new vehicles();
    }

    ReplyDelete
  2. In my last comment

    car Maruti =new vehicles();

    is a process of boxing

    ReplyDelete