0

everyone, I'm newbie. My purpose is to get the byte[] buf variable from ByteArrayInputStream by extends it, in this http://www.java2s.com/Open-Source/Android/android-core/platform-libcore/java/io/ByteArrayInputStream.java.htm tells that ByteArrayInputStream has no no-args constructor, but when I code:

class Test extends ByteArrayInputStream {
    public Test(int i){}
}

eclipse tells me: Implicit super constructor ByteArrayInputStream() is undefined. Must explicitly invoke another constructor. Before ask this I searched google then got these: Java error: Implicit super constructor is undefined for default constructor , it tells that if class B extends class A, then class A has to define a no-args constructor. OK, this easy for the classes we wrote, but what about classes from Sun's package... I wonder about this too

Thanks in advance.

Community
  • 1
  • 1
eta99
  • 15
  • 4

2 Answers2

2

two concepts to remember:

  1. by default, any subclass constructor calls no-arg constructor of super class.
  2. if there is even a single constructor defined in a class, jvm doesn't provide a no-arg constructor.

here, public Test(int i) will call ByteArrayInputStream(), which doesn't exist. So u must call any existing constructor of ByteArrayInputStream in Test(int i)'s 1st statement as like super(required_parameters);

Ankit
  • 6,554
  • 6
  • 49
  • 71
0

Your Test class should have at least two constructors :

public class Test extends ByteArrayInputStream {
    public Test(byte[] buf) {
        super(buf);
    }

    public Test(byte[] buf, int offset, int length) {
        super(buf, offset, length);
    }
}
Omar MEBARKI
  • 647
  • 4
  • 8