ChangUZ ChangUZ - 1 year ago 74
Java Question

What is interface and wrapper?

I know why use interface and wrapper.

But I confuse to name wrapper class... ("Who is wrapper?" I see I do not know well...)

public Interface A {
void method();

public Class B implements A {
void method() {

I am confused by two things...

  1. Wrapper Class is class , so B is wrapper.

  2. We usually see(or think?) a.method() not b.method(), so A is wrapper.

What is wrapper?

A? B?


How to name A,B good using "Wrapper" or "W"?

A, AWrapper? or B, BWrapper? or others...?

Answer Source

A is an interface. B is a concrete implementation of Interface. Nothing else can be said about them from the code you provided.

A Wrapper "wraps" the functionality of another class or API by adding or simplifying the functionality of the wrapped class/API. For example, the Primitive Wrappers from Java add useful methods like doubleValue and compareTo to Java primitives.

You're thinking of Polymorphism.

That's what allows us to say things like:

A a = new B();
B b = new B();
List<A> stuffs = new ArrayList<A>();

Side note:

Interface and Class are not allowed to be capitalize in Java. Your declarations should be like so:

public interface A {
  // methods

public class B implements A {
  // methods