YouTip LogoYouTip

Kotlin Extend

All classes in Kotlin inherit from the Any class, which is the superclass of all classes. For classes without a supertype declaration, Any is the default superclass: class Example // Implicitly inherits from Any Any provides three functions by default: equals() hashCode() toString() Note: Any is not java.lang.Object. If a class is to be inherited, it can be modified with the open keyword. open class Base(p: Int) // Base class class Derived(p: Int) : Base(p) * * * ## Constructors ### Subclass Has Primary Constructor If the subclass has a primary constructor, the base class must be initialized immediately in the primary constructor. open class Person(var name : String, var age : Int){// Base class} class Student(name : String, age : Int, var no : String, var score : Int) : Person(name, age) {} // Test fun main(args: Array) { val s = Student("Tutorial", 18, "S12346", 89) println("Name: ${s.name}") println("Age: ${s.age}") println("Student No: ${s.no}") println("Score: ${s.score}") } Output: Name: Tutorial Age: 18 Student No: S12346 Score: 89 ### Subclass Does Not Have Primary Constructor If the subclass does not have a primary constructor, then you must initialize the base class in each secondary constructor using the super keyword, or delegate to another constructor. When initializing the base class, you can call different constructors of the base class. class Student : Person { constructor(ctx: Context) : super(ctx) { } constructor(ctx: Context, attrs: AttributeSet) : super(ctx,attrs) { } } ### Example /**User base class**/ open class Person(name:String){ /**Secondary constructor**/ constructor(name:String,age:Int):this(name){ //Initialize println("-------Base class secondary constructor---------") } } /**Subclass inherits Person class**/ class Student:Person{ /**Secondary constructor**/ constructor(name:String,age:Int,no:String,score:Int):super(name,age){ println("-------Subclass secondary constructor---------") println("Name: ${name}") println("Age: ${age}") println("Student No: ${no}") println("Score: ${score}") } } fun main(args: Array) { var s = Student("Tutorial", 18, "S12345", 89) } Output: -------Base class secondary constructor---------------- -------Subclass secondary constructor--------- Name: Tutorial Age: 18 Student No: S12345 Score: 89 * * * ## Overriding In the base class, when a function is declared with fun, it is default final and cannot be overridden by subclasses. If you want to allow subclasses to override the function, you must manually add the open modifier to it. Subclasses use the override keyword to override methods: /**User base class**/ open class Person{ open fun study(){ // Allow subclasses to override println("I graduated") } } /**Subclass inherits Person class**/ class Student : Person() { override fun study(){ // Override method println("I'm in college") } } fun main(args: Array) { val s = Student() s.study(); } Output: I'm in college If there are multiple same methods (inherited or implemented from other classes, such as A and B classes), then you must override that method, and use super generic to selectively call the parent class's implementation. open class A { open fun f () { print("A") } fun a() { print("a") } } interface B { fun f() { print("B") } // Interface members are open by default fun b() { print("b") } } class C() : A() , B{ override fun f() { super.f()//Call A.f() super.f()//Call B.f() } } fun main(args: Array) { val c = C() c.f(); } C inherits from a() or b(). C can not only inherit functions from A or B, but C can also inherit functions common to both A() and B(). At this time, there is only one implementation of that function in C. To eliminate ambiguity, that function must call the implementation of that function in both A() and B() and provide its own implementation. Output: AB * * * ## Property Overriding Property overriding uses the override keyword, and properties must have compatible types. Each declared property can be overridden by an initializer or getter method: open class Foo
← Kotlin GenericsKotlin Basic Types β†’