rangga_hrdme

OOP: CLASS, INHERITANCE

Apr 20th, 2021
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.78 KB | None | 0 0
  1. /* INHERITANCE
  2. *  Create sub class from main class (Inheritance)
  3. *  Create object from made class
  4. * */
  5.  
  6. open class oop_vehicle {
  7.     var name: String? = null
  8.     var color: String? = null
  9.     var wheels: Int? = null
  10.  
  11.     constructor()
  12.  
  13.     // Constructor
  14.     constructor(name: String, color: String, wheels: Int) : this() {
  15.         this.color = color
  16.         this.name = name
  17.         this.wheels = wheels
  18.  
  19.         println("Object, wheels: " + this.wheels + " name: " + this.name + " color: " + this.color)
  20.     }
  21. }
  22.  
  23. // Inheritance ( : )
  24. class car() : oop_vehicle()
  25.  
  26. fun main() {
  27.     // Create object
  28.     var sedan = oop_vehicle("bmw", "red", 4)
  29.     // Create object from class: car
  30.     var cars = car()
  31.     cars.wheels = 8
  32.     println("Wheels: ${cars.wheels}")
  33. }
  34.  
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment