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...?

2 Answers 2

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
}
1
  • Thanks! Do you know other examples (not Primitive Wrappers)?
    – ChangUZ
    Dec 6, 2012 at 4:30
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.