how to create wrapper class for any user defined class
Asked Answered
K

3

18

someone told me that we can create wrapper class for any user defined class instead of only for primitives, if yes! then how can we create it, i have no idea about that from where to start, would you please provide any demo code for this purpose. awaiting for your responses....

Koball answered 20/4, 2011 at 14:20 Comment(4)
Homework? Anyways, do you mean you want the Java program to be creating wrapper classes on-the-fly, at runtime? Or do you mean that as part of the program you're writing you want to write some wrapper classes?Narwhal
@Narwhal yaa! i want to create it as a part of program but i don't have any idea from where to start?? :(Koball
Question does not make sense. What is a "wrapper" class for a "user-defined" class? Do you have an example in mind?Bindery
Perhaps you could start with a concrete class you would like to wrap.Guttersnipe
A
15

The term 'wrapping' sometimes means the same thing as encapsulation, where an object or type is used internally by a class as part of its implementation details, and doesn't expose it to outside code. However, wrapping often refers specifically to the act of encapsulating a class in another class which implements the same interface as the wrapped class but changes its behaviour slightly or adds new features (Decorator Pattern), or the outer class implements a different interface, essentially converting the wrapped class to make it compatible with another program (Adapter Pattern). Both of these types of wrapping are nearly always done manually, and must be done at compile-time (by writing code).

You can also generate dynamic proxies for virtually any object at runtime using java.lang.reflect.Proxy.newProxyInstance(...). You can read the official guide on Dynamic Proxy Classes to learn how to use it. However, you haven't given any use cases yet, so this might not be what you're looking for. Proxies are usually reserved for protecting objects or delegating to a remote server via RPC, and can be very complex.

Abdicate answered 20/4, 2011 at 16:22 Comment(0)
N
0

wrapper class-wrapper class is that which is used to wrap or encapsulate primitive data to instantiated objects in other classes

public class wrapperdemo {

    public static void main(String[] args) {
        //integer case
        //primitive type
        int i=20;
        //reference type
        //(explicit declaration of the primitive to reference object)
        Integer iref =new Integer(i);
        //boxing(changing primitive to reference variable)
        System.out.println(iref);
        //unboxing(fetching primitive out of reference variable)
        int j=iref.intValue();
        System.out.println(j);  

        int k = 40;
        //(implicit declaration of primitive to refernce object)
        Integer kref=k;                  //boxing
        System.out.println(kref);
        int k1=kref.intValue();
        System.out.println(k1);         //unboxing

        //character case
        //explicit conversion of char primitive to refernec object
        char ch='D';
        Character cref=new Character(ch);   //boxing
        System.out.print(cref);
        /*char ch1=cref.charValue();          //unboxing
        System.out.println(ch1);*/

        //implicit conversion
        char c='S';
        Character ccref=c;                 //boxing
        System.out.print(ccref);
        /*char cc1=ccref.charValue();
        System.out.println(cc1);*/
        }
}
Norwegian answered 23/6, 2017 at 19:22 Comment(0)
M
0

Sample code for creating wrapper :

import java.util.ArrayList;
import java.util.List;


class IntVal {
    private int i;
    public IntVal(int a) {
        i = a;
    }

    public int getVal() {
        return i;
    }

    public void setValue(int a) {
        this.i = a;
    }

    public void increment() {
        i++;
    }

    @Override
    public String toString() {
        return Integer.toString(i);
    }
}

public class WrapperClass {

    public static void main(String[] args) {



  List list = new ArrayList();

  for (int i = 0; i < 10; i++) {
      list.add(new IntVal(i)); 
  }
  System.out.println(list);
  for (int i = 0; i < list.size(); i++) {
      ((IntVal) list.get(i)).increment();
  }

  System.out.println(list);

    }
}
Mireille answered 5/8, 2019 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.