Java ByteArrayInputStream Implicit super constructor is undefined. Must explicitly invoke another constructor

StackOverflow https://stackoverflow.com/questions/15426525

  •  23-03-2022
  •  | 
  •  

Question

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.

Was it helpful?

Solution 2

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);
    }
}

OTHER TIPS

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);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top