Possible Duplicate:
Why are these variables of an interface type not being used to instantiate new objects?
In the code sample below from an API that I’m using, there is an initialization of the variables, “audiodecoder” and “cc”. The two variables are of an interface type. However, the keyword implements is not used anywhere in the code sample for any thing but actionlisteners.
This goes against anything I’ve learned about Java so far. I’ve listed the import statements below in the thinking that maybe they hold some clue about why IDecoder and ICodecContextWrapper do not have corresponding implements statements. I’ve also tried to find some documentation on using interfaces in this manner with no success. Could someone explain to me why the keyword implements is not used? Is there a name for this concept and, perhaps some documentation that you know of in regards to the concept?
edit: I should also add that the variables are not declared in the sample code with the keyword new either.
import org.libav.audio.Frame2AudioFrameAdapter;
import org.libav.audio.PlaybackMixer;
import org.libav.audio.SampleInputStream;
import org.libav.avcodec.ICodecContextWrapper;
import org.libav.avformat.IChapterWrapper;
import org.libav.avformat.IFormatContextWrapper;
import org.libav.avformat.IStreamWrapper;
import org.libav.avresample.bridge.AVResampleLibrary;
import org.libav.avutil.IDictionaryWrapper;
import org.libav.avutil.bridge.AVChannelLayout;
import org.libav.avutil.bridge.AVSampleFormat;
import org.libav.bridge.LibraryManager;
import org.libav.data.IFrameConsumer;
import org.libav.util.swing.VideoPane;
IDecoder audioDecoder = player.getAudioStreamDecoder(streamIndex);
ICodecContextWrapper cc = audioDecoder.getCodecContext();
This
means that whatever is returned implements
IDecoder.e.g.
Note the distinction in types between what’s constructed, and the returned reference.
The object returned will be a concrete class, but since it implements that interface, it can be referred to by that interface. It could implement other interfaces too, and could have functionality exposed, but you’ll only be able to access it as a
IDecoder