TheBulgarianWolf

Kotlin Inheritance

Apr 4th, 2021
410
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open class Person(age: Int,name:String){
  2.     init{
  3.         println("My name is $name.")
  4.         println("My age is $age.")
  5.     }
  6. }
  7.  
  8. class MathTeacher(age: Int,name:String) : Person(age,name){
  9.     fun teachMaths(){
  10.         println("I teach in a primary school.")
  11.     }
  12. }
  13.  
  14. class Footballer(age: Int,name: String) : Person(age,name){
  15.     fun playFootball(){
  16.         println("I play in LA Galaxy")
  17.     }
  18. }
  19.  
  20. open class Log{
  21.     var data: String = ""
  22.     var numberOfData = 0
  23.     constructor(_data: String){
  24.  
  25.     }
  26.  
  27.     constructor(_data: String, _numberOfData: Int){
  28.         data = _data
  29.         numberOfData = _numberOfData
  30.         println("$data: $numberOfData times")
  31.     }
  32. }
  33.  
  34. class AuthLog : Log{
  35.     constructor(_data: String) : this("From AuthLog -> $_data", 10){
  36.  
  37.     }
  38.  
  39.     constructor(_data: String,_numberOfData: Int) : super(_data,_numberOfData){
  40.  
  41.     }
  42. }
  43.  
  44. fun main(args: Array<String>){
  45.     val t1 = MathTeacher(25,"Jack")
  46.     t1.teachMaths()
  47.  
  48.     println()
  49.  
  50.     val f1 = Footballer(29,"Cristiano")
  51.     f1.playFootball()
  52.  
  53.     println()
  54.  
  55.     val p1 = AuthLog("Bad Password")
  56. }
RAW Paste Data