1

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() {
        doSomething();
    }
}

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?

And...

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

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

Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
ChangUZ
  • 5,380
  • 10
  • 46
  • 64

2 Answers2

5

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>();
stuffs.add(b);

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
}
Community
  • 1
  • 1
Tim Pote
  • 27,191
  • 6
  • 63
  • 65
3

Neither A or B from your example can be called a wrapper. The relationship between A and B is Inheritance. A Wrapper class usually contains one or more Wrappee objects. Adapter and Facade patterns are good examples of wrappers.

See this for a detailed discussion on Wrappers

Community
  • 1
  • 1
Sameer
  • 4,379
  • 1
  • 23
  • 23