Kotlin Inheritance
In Kotlin, all classes inherit from the Any
class, which is the superclass of all classes and the default superclass for classes without an explicit superclass declaration:
class Example // Implicitly inherits from Any
Any
provides three default functions:
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) // Define base class
class Derived(p: Int) : Base(p)
Constructors
Subclass has a primary constructor
If a 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<String>) {
val s = Student("tutorialpro", 18, "S12346", 89)
println("Student Name: ${s.name}")
println("Age: ${s.age}")
println("Student Number: ${s.no}")
println("Score: ${s.score}")
}
Output:
Student Name: tutorialpro
Age: 18
Student Number: S12346
Score: 89
Subclass does not have a primary constructor
If a subclass does not have a primary constructor, the base class must be initialized with the super
keyword in each secondary constructor, or by delegating to another constructor. When initializing the base class, different constructors of the base class can be called.
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){
// Initialization
println("------- Base class secondary constructor ---------")
}
}
/** Subclass inherits from Person class **/
class Student:Person{
/** Secondary constructor **/
constructor(name:String,age:Int,no:String,score:Int):super(name,age){
println("------- Inherited class secondary constructor ---------")
println("Student Name: ${name}")
println("Age: ${age}")
println("Student Number: ${no}")
println("Score: ${score}")
}
}
fun main(args: Array<String>) {
var s = Student("tutorialpro", 18, "S12345", 89)
}
Output:
------- Base class secondary constructor ---------
------- Inherited class secondary constructor ---------
Student Name: tutorialpro
Age: 18
Student Number: S12345
Score: 89
Overriding
In the base class, when a function is declared with fun
, this function is by default final and cannot be overridden by a subclass. If you want to allow the subclass to override this function, you must manually add the open
modifier to it. Subclasses override methods using the override
keyword:
/** User base class **/
open class Person{
open fun study(){ // Allow subclasses to override
println("I graduated")
}
}
/** Subclass inherits from Person class **/
class Student : Person() {
override fun study(){ // Overriding method
println("I am in college")
}
}
fun main(args: Array<String>) {
val s = Student()
s.study();
}
Output:
I am in college
If there are multiple identical methods (inherited or implemented from other classes, such as classes A and B), then the method must be overridden, using the super
generic to selectively call the implementation of the parent class.
open class A {
open fun f () { print("A") }
fun a() { print("a") }
}
interface B {
fun f() { print("B") } // Interface member variables are open by default
fun b() { print("b") }
}
class C() : A() , B{
override fun f() {
super<A>.f()// Call A.f()
super<B>.f()// Call B.f()
}
}
fun main(args: Array<String>) {
val c = C()
c.f();
}
C inherits from a() or b(), C can not only inherit functions