I read this code in a book just now.
public class AdapterWrapper implements ListAdapter {
ListAdapter delegate=null;
// other code
}
The ListAdapter is a public interface and a reference of that interface has been made and assigned null. Is this valid? I’m really confused by this.
Yes, it’s valid. It just means that
delegateis a reference toListAdapterinterface and it’s currently pointing atnull.You can later make it point to any class implementing the
ListAdapterinterface, such asSimpleCursorAdapter,WrapperListAdapteror any other implementation you want.Using an interface type as reference is useful when you don’t know ahead of time what class is it going to be at runtime. So you just use a reference pointing to an interface.