Recently I have seen a lot of interesting topics about Kotlin on portal.kotlin-academy.com/#/. Personally, I think it is very suitable for Kotlin lovers. Interested partners can consult it by themselves. The interesting Kotlin series notes your understanding of each question.

0x05: Making open abstract

open class A {
    open fun a(a){}}abstract class B: A() {
    abstract override fun a(a)
}

open class C: B(a)Copy the code

What is the result of running the above code? Optional:

  1. Compiles fine
  2. Error: Class ‘C’ is not abstract and does not implement abstract base class member
  3. Error: ‘a’ overrides nothing
  4. Error: Function ‘a’ must have a body

Think about it and write down the answers in your mind.

Analysis of the

The open keyword in Kotlin is used on a class to indicate that the class can be inherited, and on a function to indicate that the function can be overridden. This is not necessary in Java, where the default is to create arbitrary subclasses and override arbitrary methods, unless the final keyword is explicitly used to indicate that the class is not inherited and the function is not overridden. In Kotlin, classes are final by default and methods are final by default, hence the open keyword.

The abstract keyword in Kotlin can modify classes, functions, and attributes to represent abstract classes, abstract functions, and abstract attributes, respectively. Abstract functions are not allowed to be implemented in abstract classes and abstract properties are not allowed to be initialized in abstract classes. We also don’t need to decorate abstract classes, functions, and properties with the open keyword to tell the compiler that they can be inherited or overridden, because the point of abstraction is to be implemented.

So, the way they wrote A and B is correct. The key is to write C:

abstract class B: A() {
    abstract override fun a(a)
}

open class C: B(a)Copy the code

Class C inherits abstract class B, and class C is not an abstract class, so class C must implement abstract function A () in B, which is not implemented in the problem. Therefore, according to our analysis, the code in the problem will compile and report an error, and the reason for the error is that class C does not implement abstract method a() in class B. The correct answer is:

Error: Class ‘C’ is not abstract and does not implement the abstract base Class member

Knowing why, it’s obvious how to tweak the code to make it compile correctly, either by modifying class B to kill the abstract function or override function A (), or by modifying class C to implement the abstract function or make it abstract.

Method one:

open class A {
    open fun a(a){}}abstract class B : A() {}open class C : B(a)Copy the code

Method 2:

open class A {
    open fun a(a){}}abstract class B: A() {
    abstract override fun a(a)
}

open class C: B() {
    override fun a(a) {
        println("a impl")}}Copy the code

Solution 3:

open class A {
    open fun a(a){}}abstract class B: A() {
    abstract override fun a(a)
}

abstract class C: B(a)Copy the code

conclusion

  1. The default class in Kotlin isfinalFunctions and properties are not inherited;
  2. The parent class is abstract class, if the subclass is not abstract class, must implement all the abstract methods and abstract properties in the parent class; If the subclass is abstract, this is not necessary;
  3. Abstract classes, abstract functions, and abstract classes are no longer neededopenKeyword modification;
  4. openFunctions can be rewritten as abstract functions.