Tags: abstract, bytebuffer, classes, cross, extend, extending, java, origanally, thispublic, wrong
Extending Abstract Classes such I ByteBuffer?
On Java Studio » Java Knowledge
6,964 words with 8 Comments; publish: Sat, 22 Sep 2007 13:14:00 GMT; (15078.13, « »)
Sorry for the cross post but I think I posted this in the wrong forum origanally.
I need to extend ByteBuffer so I did so like this:
public abstract class SerialPortBuffer extends ByteBuffer {
}
but now I am getting:
Implicit super constructor ByteBuffer() is undefined for default constructor. Must define an explicit constructor
How do I fix this? Sorry for the newbie questions and the goof on the cross post.
http://java-knowledge.developerfaqs.com/q_java-tech_26034.html
All Comments
Leave a comment...
- 8 Comments

ByteBuffer does not have a constructor public ByteBuffer()
public abstract class SerialPortBuffer extends ByteBuffer {
}
is equivalent to
public abstract class SerialPortBuffer extends ByteBuffer {
public SerialPortBuffer() {
super(); //This is where the error is
}
}
Explicitly define a construtor to fix the problem.
public abstract class SerialPortBuffer extends ByteBuffer {
public SerialPortBuffer() { }
}
#1; Wed, 04 Jul 2007 20:21:00 GMT

> ByteBuffer does not have a constructor
> public ByteBuffer()
> > public abstract class SerialPortBuffer extends
> ByteBuffer {
> }
>
> is equivalent to
> > public abstract class SerialPortBuffer extends
> ByteBuffer {
> public SerialPortBuffer() {
> super(); //This is where the error is
> }
> }
>
> Explicitly define a construtor to fix the problem.
> > public abstract class SerialPortBuffer extends
> ByteBuffer {
> public SerialPortBuffer() { }
> }
>
But I dodnot think defining a no args constructor explicitly would remove the problem, since the no args constructor is still undefined in the super class. You need to define a constructor woth some arguments
#2; Wed, 04 Jul 2007 20:21:00 GMT

- Sorry this did not work.Now I get : Implicit super constructor ByteBuffer() is undefined. Must explicitly invoke another constructor #3; Wed, 04 Jul 2007 20:21:00 GMT

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Explicitly define a construtor to fix the problem.
public abstract class SerialPortBuffer extends ByteBuffer {
public SerialPortBuffer() { }
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Sorry this did not work.
Now I get :
Implicit super constructor ByteBuffer() is undefined. Must explicitly invoke another constructor
#4; Wed, 04 Jul 2007 20:21:00 GMT

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
But I dodnot think defining a no args constructor explicitly would remove the problem, since the no args constructor is still undefined in the super class. You need to define a constructor woth some arguments
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public abstract class SerialPortBuffer extends ByteBuffer {
/**
* .java-knowledge.developerfaqs.com.param arg0
* .java-knowledge.developerfaqs.com.param arg1
* .java-knowledge.developerfaqs.com.param arg2
* .java-knowledge.developerfaqs.com.param arg3
*/
SerialPortBuffer(int arg0, int arg1, int arg2, int arg3) {
super(arg0, arg1, arg2, arg3);
// TODO Auto-generated constructor stub
}
When I try this I get.
The constructor ByteBuffer(int, int, int, int) is not visibe
Can anyone tell me how to extend ByteBuffer?
#5; Wed, 04 Jul 2007 20:21:00 GMT

You go to the API documentation and look there to see which of its constructors you can call in your subclass's constructor.
Then you see that there aren't any. This means that all of its constructors are private, which in turn means you can't extend it.
Is there a reason you must extend it? It's quite likely that composition would be more appropriate anyway.
PC²
#6; Wed, 04 Jul 2007 20:21:00 GMT

Thanks!
Strange Core Java and another text never mention compostion. I guess I have used it but never new it was called that. Isn't this similar to enapsulation? Or is encapsulation techniclly hiding the various member variables?
Well this is what I did (hope is what you meant)
public abstract class SerialPortBuffer {
private ByteBuffer buf;
public SerialPortBuffer() {
buf = ByteBuffer.allocate(256);
}
public double getDouble() {
return buf.getDouble();
}
#7; Wed, 04 Jul 2007 20:21:00 GMT

- Yep, that's composition. Your class didn't really need to BE a ByteBuffer, it only needed to HAVE one. BE is inheritance, HAVE is composition. #8; Wed, 04 Jul 2007 20:21:00 GMT