concept class in category kotlin

appears as: class, classes, class, The class, classes
The Joy of Kotlin

This is an excerpt from Manning's book The Joy of Kotlin.

Tests are created in the same place as standard JUnit tests: in the src/test/kotlin directory of each subproject. If you’re using IntelliJ, click the function to test, type Alt+Enter, and select Create Test. In the dialog box, select a version of JUnit. IntelliJ will complain that this version is absent and will propose to fix this. Ignore this error and check that the destination package is in the correct branch (java or kotlin, in case of a mixed project). Change the proposed name for the class if you want, but don’t select any function to test, and then click OK. IntelliJ creates an empty file in the correct package.

  • A Kotlin class is public by default, so there’s no need for the word public. To make a class non-public, you can use the private, protected, or internal modifiers. The internal modifier means that the class is accessible only from inside the module where it’s defined. There’s no Kotlin equivalent for "package private" (corresponding to the absence of a modifier) in Java. Unlike Java, protected is restricted to extending classes and doesn’t include classes in the same package.
  • A Kotlin class is final by default, so the equivalent Java class would be declared with the final modifier. In Java, most classes should be declared final but programmers often forget to do that. Kotlin solves this problem by making classes final by default. To make a Kotlin class non-final, use the open modifier. This is much safer because classes opened for extension should be specifically designed for this.
  • Extending a class uses the same syntax. The difference is that the extended class name is followed by the parameter names enclosed in parentheses:

    class Member(name: String, registered: Instant) : Person(name, registered)

    Remember, however, that classes are final by default. For this example to compile, the extended class must be declared open, which means open for extension:

    open class Person(val name: String, val registered: Instant)

    A good programming practice is to allow extension only for classes that have been specifically designed for it. As you can see, Kotlin, unlike Java, tries to enforce this principle by not letting you extend a class if it hasn’t been designed for extension.