• All classes have a common superclass, Any. A class that has no declared superclass is Any

  • All classes are final by default and are not allowed to be inherited by default. To allow inheritance, mark it with the open keyword

  • The syntax for inheriting a superclass is to add it to:, as follows

    class SubInitOrderDemo(name:String):InitOrderDeme(name){
    ​
    }
    Copy the code
  • When inheriting a parent class, there are two things to note about the initialization of the parent class

    • If a subclass has a primary constructor, its parent class must be initialized with the arguments to the subclass’s primary constructor

    • If a subclass does not have a primary constructor, then each subconstructor in the subclass must either initialize the parent class with the super keyword or delegate to another subconstructor to do so. Of course, different subconstructors can call different constructors in the parent class at this point.

      class MyView : View {
          constructor(ctx: Context) : super(ctx)
          constructor(ctx: Context, attrs: AttributeSet) : super(ctx, attrs)
      }
      Copy the code
The constructor of the parent class must be called so that the parent class can be initialized when the child class is initialized.
Override method
Open class InitOrderDeme(name:String) :Any() {// Described by LXJ on 2021/2/5 15:40 Open Fun SecondMethod (){println(" secondMethod in base class ")}} class SubInitOrderDemo(name:String):InitOrderDeme(name) { //1. You can override only the open keyword of the base class, and the base class must be declared open. // The method must be overridden with the keyword override. //3, Can access the method or attribute of the parent class by super keyword Described by LXJ on 2021/2/5 15:41 Override fun secondMethod() {super.secondMethod() Println (super.firstProperty) println(" second method in subclasses ")}}Copy the code

The points to note in the override method are:

  • Only open methods of the open class can be overridden
  • Overriding methods in subclasses must be qualified with the keyword override
  • Overriding methods in subclasses may continue to be overridden by their subclasses, and if you want to disallow overrides, you can use the keyword final
Covering properties

Override properties similar to override methods, subclasses can override properties that their parent class overrides with the open keyword using the keyword override.

Class initialization order

When a subclass is constructed, its parent class is initialized first.

Therefore, if you call a method or property modified by open during the initialization of the parent class, it can lead to errors, so avoid using the open method or property in constructors, property initializers, and init blocks when designing the parent class

Invoke the implementation of the parent class
  • Code in a subclass can use the super keyword to call methods or properties of the parent class.
  • Accessing the superclass of an external class in an inner class can be done with the super keyword qualified by the name of the external class. Syntax for the super @ Outer
class SubInitOrderDemo(name:String):InitOrderDeme(name) { override fun secondMethod() { super.secondMethod() // Println (super.firstProperty) println(" second method in subclass ")} inner class InnerOrderDemo(){fun method(){inner class InnerOrderDemo(){fun method(){ / / use this call, it must be in the inner class in 2021/6/21 [email protected] ()}}}Copy the code
Override rules for calling methods when inheriting both classes and interfaces
open class SubInitOrderDemo(name:String):InitOrderDeme(name) { override fun secondMethod() { super.secondMethod() // Println (" second method in subclass ")}} interface Order{fun secondMethod(){print("Order interface secondMethod") } } class OtherOrderDemo(name: String):SubInitOrderDemo(name),Order{override fun secondMethod() {// call method 2021/6/21 in SubInitOrderDemo Super <SubInitOrderDemo>.secondMethod()Copy the code

To indicate which method of the supertype to call, the super keyword, delimited by Angle brackets, is implemented with the syntax super.method()