inheritance

All kotlin classes have a common superclass, Any, just like Object in Java. By default, kotlin classes are public final and uninheritable

// Indicates inheritable
open class Base {}Copy the code

Kotlin uses: to represent inheritance, similar to extends in Java

If a subclass has a main constructor

If a subclass has a primary constructor, the parent class needs to be initialized immediately in the primary constructor

open class Base(a :Int) {
    open fun text(a){}}class MyBase(a : Int ) :Base(a) {

    override fun text(a){}}Copy the code

If the subclass doesn’t have a main constructor

If the subclass does not have a primary constructor, then you need to initialize the base class in the subconstructor using the surper keyword, or delegate = to another constructor. Note that in this case, different subconstructors can call different constructors of the base class

class MyView : View {

    constructor(con: Context) : super(con){

    }

    constructor(con: Context,abb:AttributeSet) : super(con,abb){

    }
}
Copy the code

Override method

Methods are final and non-inheritable by default, so they need to use the open keyword if they want to inherit. Methods overridden by subclasses need to use the override modifier, otherwise an error will be reported. Override itself is open, which means that subclasses can override methods identified by Override

open class Base(a :Int) {
    open fun text(a){}}class MyBase(a : Int ) :Base(a) {

    override fun text(a){}}Copy the code

Methods in final classes that are decorated with open will not work

Covering properties

Attributes and methods are similar. Overriding attributes also need to be identified by Override

open class Base(a :Int) {
    open var text : Int =1
}

class MyBase(a: Int) : Base(a) {
    override var text: Int = 2
}
Copy the code

We can override val with var, but we can’t override val with var, because val has a get and no set. Var just adds a set method

An abstract class

Classes can be declared as abstract classes. Abstract classes serve as templates for subclasses and can serve as templates for public methods

  • Abstract classes, abstract properties, and abstract methods are not requiredopenThe default is open. Open and abstract cannot coexist
  • Abstract classes cannot be instantiated, but can have constructors, which are used by subclasses
  • Abstract classes can contain: properties (abstract properties or non-abstract properties), methods (abstract methods and non-abstract methods), constructors and initializer blocks, nested classes (interface enumeration), and 5 members
  • Abstract cannot modify local variables

Abstract members

  • An abstract method cannot have a method body
  • An abstract member cannot be initialized

Defining an Abstract class

abstract class A {
    // Abstract member variables cannot be initialized
    abstract var A :Int
    // Abstract methods cannot have method bodies
    abstract fun text(a)
    // Common attributes can also be overridden, requiring the open modifier
    open var B :Int  =1
    // Normal methods can also be overridden, requiring the open modifier
    open fun text1(a){}constructor() {Abstract classes cannot be instantiated. Constructors are for subclasses}}Copy the code

Seal type

The sealed class is a special abstract class used to derive a subclass using the sealed modifier

The characteristics of

The subclass of the sealed class is fixed for the following reasons

  • The subclasses of the sealed class must be in the same folder as the sealed class
  • In other folders, cannot be a subclass of a sealed class (but a subclass of a sealed class can be inherited by another class)
sealed class B {
    abstract fun text(a)
}
Copy the code

interface

Defines rules for how the system interacts with the outside world, specifying that implementers must provide certain methods to the outside world

The characteristics of

  • The default modifier is public
  • Interfaces can only inherit from interfaces, not from classes
  • An interface can define both abstract and non-abstract methods
  • Abstract and non-abstract attributes can be defined in an interface, but there are no hidden fields in the interface, so you need to provide get-set methods
  • Attributes (method), interface of the abstract members can use the public | private two kinds of modifiers, (Java automatically for members to public, if manually specify is public), abstract members only to public
  • Interfaces do not contain constructors and initializers, but interfaces can include methods (abstract/non-abstract methods), properties (abstract/non-abstract properties) nested classes (nested interfaces and enumerations)
  • The kotlin interface abstracts the memberabstractThe keyword can be omitted from the abstract classabstractCan not be omitted)
  • Interfaces can be multiple inherited
  • Interfaces cannot be instantiated, but variables can be assigned
 interface A {
    // The abstract attribute abstract can be omitted
    abstract var a: Int
    // Non-abstract attributes, useless
    var b : Int
        get() = 1
        set(value) = TODO()

    // The abstract method abstract can be used
    abstract fun text(a)
    
    // Non-abstract methods
    fun text1(a){}}Copy the code

The difference between interfaces and abstract classes

Grammatical differences

  • Abstract class with constructors
  • The interface has no constructor
  • Interfaces can be multiple inherited

Design differences

  • Abstract class representationIsn't itThe relationship between
  • interface-representedIs there anyThe relationship between
  • An interface is an abstraction of behavior
  • An abstract class is an abstraction of the class as a whole. For example, we extract some common methods into the abstract class
  • Such asThe planeThe birdThey all have the ability to fly and design interfacesFlyIf it isfighter.A hummingbirdYou can inherit abstract classes directlyThe plane, andThe bird