jeudi 8 juillet 2021

How to call private constructor of super class from child class constructor?

In Java, I'm trying to override a class coming from a library. One of the constructors of the class is private and thus I'm not able to call it from my class. Is there a way to work around this (reflection?)?

public class LibraryClass extends ProtectedLibraryClass {
  public LibraryClass() {
    super();
  }

  private LibraryClass(Boolean useFeature) {
    super(useFeature);
  }

  // Other methods
}

public class MyClass extends LibraryClass {
  
  public MyClass() {
    super();
  }

  private MyClass(Boolean useFeature) {
    super(useFeature); // <-- This line throws exception as super class constructor is private
  }
  // Override other methods
}

I can't just call super() and then set useFeature flag as useFeature flag is final in protectedLibraryClass and is set only through it's constructor.





Aucun commentaire:

Enregistrer un commentaire