0

I was trying to extend ByteBuffer class to add a "getString" method to it. But I just can't make it work.

My class is also abstract. When I extend it I see an error "There is no default constructor available in java.nio.ByteBuffer". And I think I understand why that happens as there is no public constructor in ByteBuffer class.

So how do I extend it? How can I add functionality to it?

Justas S
  • 584
  • 4
  • 12
  • 34
  • OK, there *are no accessible ctors in ByteBuffer at all.* You can't do that. Classes with only private ctors are not extensible. It's the same as a final class. – markspace Oct 26 '14 at 20:54
  • Well I did not add my own constructor as I don't need one. I'm only adding one method. I tried calling my super class constructor but another error appears saying I can't access it from outside of its package – Justas S Oct 26 '14 at 20:55

1 Answers1

3

You cannot extend ByteBuffer. Although it is not a final class, all it's constructors are package-private, which means that it can only be extended by classes in the same package (java.nio). The reason for this is that all constructors of a sub-class always need to call one constructor of the super class, and your class is not allowed to call any one of them.

If you want a special buffer, you need to directly extend Buffer, although I would not recommend this. Instead, the best solution for you is probably to write a wrapper class that delegates the methods you need to a ByteBuffer instance, or just put your getString method in another class.

Philipp Wendler
  • 11,184
  • 7
  • 52
  • 87
  • i was afraid that this is the case... I will look into these wrapper classes. I considered putting it in another class but it just doesn't _feel_ right. – Justas S Oct 26 '14 at 21:05