1

I need to extend class A but it needs to override its constructor.

package com.example.io; // Package belongs to jar from maven dependency

public class A {
    A(String s, Integer i) {
        // constructor code
    }

    // other methods
}

As it is default constructor (a constructor with only package access) I cannot access outside the package, so I created same package name in my source code com.example.io and extended class A and built successfully.

package com.example.io; // Package belongs to my source code

public class B extends A{
    B(String s, Integer i) {
        super(s, i); // Throws error on runtime
    }

    // other methods
}

But it throws runtime error saying -

java.lang.IllegalAccessError: tried to access method com.example.io.A.<init>(Ljava/lang/String;Ljava/lang/Integer;)V from class com.example.io.B

How do I solve this? I mean, is there any way I can extend class A with default constructor?

5
  • @ElliottFrisch Edited to post B Commented Nov 3, 2015 at 5:52
  • You need to delete all your *.class files, then compile again. You have old class files still around on your disk that cause this. (If it was a "real" problem, you would have gotten an error while you were compiling, not at runtime) Commented Nov 3, 2015 at 5:54
  • What do you mean by "default"? Do you mean a constructor with package access? Or do you mean a default no-arg constructor?
    – dantiston
    Commented Nov 3, 2015 at 6:11
  • @dantiston a constructor with package access Commented Nov 3, 2015 at 6:46
  • Another option is that the package is sealed, in which case you just cannot do it, but you should absolutely first try to do a clean build (deleting all old .class files from earlier compiled) like I mentioned above. Commented Nov 3, 2015 at 8:22

2 Answers 2

1

You definitely shouldn't just copy the name of the 3rd party library's package. This can lead to unexpected results. See this answer.

Instead, if the class is public, you should create your own subclass and define your own constructor. If the superclass' constructor is set to package, then you can't call it. In that case, if you have the code, you could reimplement the necessary constructor actions as needed:

public class MyClass extends A {

    String myString;
    Integer myInteger;
    String myField;

    MyClass(String s, Integer i) {
        // Can't do this because it's set to package
        // super(s, i);
        // Re-implement?
        this.myString = s;
        this.myInteger = i;
        // Implement your own stuff
        this.myField = s + String.valueOf(i);
    }
}
-2

You can use reflection:

    Constructor<A> constructor = A.class.getDeclaredConstructor(String.class, Integer.class);
    constructor.setAccessible(true);

In fact i do not think that it will work by changing package as the same as 3rd party lib. See: Possible to use two java classes with same name and same package?

Not the answer you're looking for? Browse other questions tagged or ask your own question.