0

I think this is a horribly stupid question, but I can't, for the life of me, get my head around the fact that APIs (such as the Java Sound API) and packages (such as javax.sound.sampled in the Sound API) contain interfaces and the methods in these interfaces can conveniently be called.

If an interface has no method definitions, what's being called?

Shred On
  • 23
  • 4

3 Answers3

0

You can't instantiate an interface, only a class that implements an interface.

private Clip m_clip;

This does not instantiate or call anything. It just declares a variable into which you can store a reference to an object which implements the Clip interface (or null).

Keith Randall
  • 22,985
  • 2
  • 35
  • 54
  • Stupid terminology from me again. You posted an answer just as I was editing my post for that very reason ;) – Shred On Apr 20 '12 at 16:55
0

Objects can be declared with an interface type, but can only be initialized with a concrete type. So

private Clip m_clip;

is fine, as it is only a declaration. Before using m_clip, you need to initialize it, i.e. make it point to a valid object.

m_clip = new Clip();

would produce a compilation error, as Clip, being an interface, can't be instantiated. However,

class MyClip implements Clip { ... }
...
m_clip = new MyClip();

is OK: MyClip is a concrete (non-abstract) class implementing the Clip interface, so it can be instantiated and used whenever a Clip is needed. After this statement, the static type of m_clip is Clip (this is the type which is known to the compiler at compile time, and to the clients of m_clip too), but its dynamic or runtime type is MyClip. This is not traced by the compiler, nor is it necessarily known to the clients of m_clip. This may look strange based on this simple example, but consider that the actual object may be returned from a method, which can be in another class, not written by you, so noone may know what happens inside there. The concrete type used may not even be visible to the outside world.

Once you initialized m_clip, its clients can use it (e.g. call its methods). But they only see its public interface, i.e. the methods declared in Clip. This all may sound complicated, but it has a terrific advantage: the actual (runtime) type of m_clip can be changed anytime, without affecting its clients. E.g. by changing the line above to

m_clip = new SomeOtherClip();

its implementation is changed, but its clients still can use it without changing anything in their code.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
0

You should read the documentation on the Java Sound API and its Service Provider Interface. Chapter 1 of the Java Sound API Guide explains the design of the API, the implementation and the way the implementation can be extended by different providers implementing the Service Provider Interface.

In a nutshell, the implementation of the interfaces exist, but application developers do not have direct access to these classes.

In Java 6 (in Windows), the default Java Sound implementation is implemented by the com.sun.media.sound package included in rt.jar and the DLLs named jsound*.dll. The SPI model allows the implementation to be replaced or extended by other service providers. I found this other StackOverflow answer that explains how the SPI model work in Java

Community
  • 1
  • 1
dsalazar
  • 51
  • 4
  • So where do these implementations exist? I have gone through the documentation before I posted this question but could not find it addressing my particular question which is a general one and is not related to the Java Sound API per se. I found a similar question answered on another site, regarding JDBC and the answerer mentioned that the implementations exist in the JAR files containing the JDBC driver classes. I couldn't find any mention of any JAR files relating to the Java Sound API anywhere. – Shred On Apr 21 '12 at 01:29
  • Just edited my answer to include which classes are part of the default Java Sound implementation in Java 6 in Windows – dsalazar Apr 21 '12 at 08:00