Search code examples
kotlinpublic

Make public class open internally but closed externally kotlin


I have a public abstract class which I want other classes in the module to extend, but I do not want to to be extendible externally. I am aware I could make it sealed, but it has many subclasses and would be cumbersome to have them all in the same file. How can this be achieved?


Solution

  • You can define its constructor as internal. Subclasses must call the superclass' constructor, so only files that can see that constructor will be able to subclass your class.