Abstract but not abstract?

I was looking for information about the java.nio.ByteBuffer class and noticed that it has a lot of abstract methods. What confuses me is that you can create a ByteBuffer object by calling the static ByteBuffer.allocate or ByteBuffer.allocateDirect methods, and then use the abstract methods on this object. Erhm… how is that possible?

For example:


ByteBuffer bb = ByteBuffer.allocateDirect(4); 
bb.putInt(10); // ByteBuffer.putInt(int) is an abstract method, still I can use it :/

System.out.println(bb.getInt(0)); // calling yet another abstract method =|

Here’s a link to the ByteBuffer reference: http://java.sun.com/j2se/1.5.0/docs/api/java/nio/ByteBuffer.html

That’s because the object you get from ByteBuffer.allocateDirect() is a subclass of ByteBuffer, that implements the abstract methods.

Try:
String realClassName = bb.getClass().getName();

I would suspect the object passed back from allocate() isn’t actually a ByteBuffer but a sub-class that implements the abstract methods.

Kev

Yea, you guys are right, it returns a DirectByteBuffer object.