top of page

Kotlin inheritance - Kotlin Assignment Help

Updated: Feb 15, 2023



Inheritance is an important feature of object-oriented programming languages. Inheritance allows inheriting the feature of an existing class (or base or parent class) to a new class (or derived class or child class).


The main class is called the superclass (or parent class) and the class which inherits the superclass is called the subclass (or child class). The subclass contains features of the superclass as well as its own.

open class Employee(name: String, age: Int, salary: Float) {  
    // code of employee  
}  
  
class Programmer(name: String, age: Int, salary: Float): Employee(name,age,salary) {  
    // code of programmer  
}  
  
class Salesman(name: String, age: Int, salary: Float): Employee(name,age,salary) {  
    // code of salesman  
}  



open keyword:

Kotlin classes are final by default, they cannot be inherited simply. We use the open keyword before the class to inherit a class and make it non-final,

For example:


open class Example{  
// I can now be extended!  
}  

Inheriting fields from a class

When we inherit a class to derive class, all the fields and functionalities are inherited. We can use these fields and functionalities in derived class.

open class Base{  
val x = 10  
}  
class Derived: Base() {  
    fun foo() {  
println("x is equal to " + x)  
    }  
}  
fun main(args: Array<String>) {  
val derived = Derived()  
    derived.foo()   
}  


Output:
x is equal to 10


Inheriting methods from a class

open class Bird {  
    fun fly() {  
println("flying...")  
    }  
}  
class Duck: Bird() {  
    fun swim() {  
println("swimming...")  
    }  
}  
fun main(args: Array<String>) {  
val duck = Duck()  
    duck.fly()   
duck.swim()  
}  

Output:

flying...
swimming...


Inheritance and primary constructor

If the base and derived class both have a primary constructor in that case the parameters are initialized in the primary constructor of the base class. In the above example of inheritance, all classes contain three parameters "name", "age" and "salary" and all these parameters are initialized in the primary constructor of a base class.


When a base and derived class both contain different numbers of parameters in their primary constructor then base class parameters are initialized from derived class object.


For example:

open class Employee(name: String,salary: Float) {  
init {  
println("Name is $name.")  
println("Salary is $salary")  
    }  
}  
class Programmer(name: String, dept: String, salary: Float):Employee(name,salary){  
init {  
println("Name $name of department $dept with salary $salary.")  
    }  
    fun doProgram() {  
println("Programming is my passion.")  
  
    }  
}  
class Salesman(name: String, dept: String, salary: Float):Employee(name,salary){  
init {  
println("Name $name of department $dept with salary $salary.")  
    }  
    fun fieldWork() {  
println("Travelling is my hobby.")  
  
    }  
}  
fun main(args: Array<String>){  
val obj1 = Programmer("Abhay", "Development", 40000f)  
    obj1.doProgram()  
println()  
val obj2 = Salesman("Ajay", "Marketing", 30000f)  
    obj2.fieldWork()  
}  


Output:


Name is Ashu. 
Salary is 40000.0
Name Ashu of department Development with salary 40000.0. Programming is my passion.
Name is Ajay. Salary is 30000.0 
Name Ajay of department Marketing with salary 30000.0. 
Travelling is my hobby.

Hope you understand the inheritance and syntax of kotlin in the next blog we are going to learn overloading and overriding in kotlin.

Thank you


The journey of solving bug and completing project on time in Kotlin can be challenging and lonely. If you need help regarding other sides to Kotlin, we’re here for you!


Drop an email to us at contact@codersarts.com with the Project title, deadline, and requirement files. Our email team will revert back promptly to get started on the work.

Comments


bottom of page