

Kotlin Class inheritance
Inheritance is a mechanism of acquiring the features and behaviors of a class by another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class.Kotlin Example of class inheritance
Let's take below example and try to have inheritance concept, in this example we can see that there are some common functions and attributes that we can have them in a superclass. class Car {
var color: String = "Silver" //Default value for colors cars is Silver
var make: String = ""
var model: String = ""
get() = field
set(value) {
field = value
}
}
class Truck{
var color: String = "Silver" //Default value for colors trucks is Silver
var make: String = ""
var model: String = ""
get() = field
set(value) {
field = value
}
}
fun main() {
val newCar = Car()
newCar.color = "Red"
newCar.make = "BMW"
newCar.model = "X5"
println("Car color is: " + newCar.color)
println("Car make is: " + newCar.make)
println("Car model is: " + newCar.model)
val newTruck = Truck()
newTruck.color = "White"
newTruck.make = "Chevy"
newTruck.model = "Silverado"
println("Truck color is: " + newTruck.color)
println("Truck make is: " + newTruck.make)
println("Truck model is: " + newTruck.model)
}
In the above example we can create a new class called
Vehicle
which will be inherited in classes: Car
and Truck
.open class Vehicle {
var color: String = "Silver" //Default value for colors trucks is Silver
var make: String = ""
var model: String = ""
get() = field
set(value) {
field = value
}
}
class Car : Vehicle() { }
class Truck : Vehicle() { }
fun main() {
val newCar = Car()
newCar.color = "Red"
newCar.make = "BMW"
newCar.model = "X5"
println("Car color is: " + newCar.color)
println("Car make is: " + newCar.make)
println("Car model is: " + newCar.model)
val newTruck = Truck()
newTruck.color = "White"
newTruck.make = "Chevy"
newTruck.model = "Silverado"
println("Truck color is: " + newTruck.color)
println("Truck make is: " + newTruck.make)
println("Truck model is: " + newTruck.model)
}
Output
Car color is: Red
Car make is: BMW
Car model is: X5
Truck color is: White
Truck make is: Chevy
Truck model is: Silverado
Car make is: BMW
Car model is: X5
Truck color is: White
Truck make is: Chevy
Truck model is: Silverado
Explaining class Inheritance example
We created a supper class called Vehicle using syntaxopen class
and when creating a class Car or Truck we use : Vehicle()
to point inheritance.Conclusion
In this page (written and validated by A. Gawali) you learned about Kotlin Class inheritance . What's Next? If you are interested in completing Kotlin tutorial, your next topic will be learning about: Kotlin HTTP GET request.
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.
Share On: |
Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.