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....
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.
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);*/
}
}
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);
}
}
© 2022 - 2024 — McMap. All rights reserved.