Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

443
Views
Expecting member declaration while creating class and constructor

I am newbie in kotlin, I trying to apply lesson on oop on kotlin, but I got multiple "Expecting member declaration"

enter image description here

I don't know where's error in this code

open class Car( open val color:String?=null, open val brand:String?=null) {
    
    open fun speed(){
        println("max speed is 220")
    }
}

class Toyota() : Car() {
    override color = "White"
    override brand = "Toyota"
    
    override fun speed(){
        println("max speed is 360")
    }
}


fun main() {
   
    var car:Toyota = Toyota()
    car.speed()
    
   
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You are missing a val keyword in both parameters in Toyota class:

class Toyota() : Car() {
    override val color = "White"
    override val brand = "Toyota"

    override fun speed(){
        println("max speed is 360")
    }
}

Or you can do it even better by using Car constructor directly:

class Toyota : Car(color = "White", brand = "Toyota") {
    override fun speed(){
        println("max speed is 360")
    }
}

With this approach you can even make Car simpler (no need for open keyword on your properties):

open class Car(val color : String? = null, val brand : String? = null) {
    open fun speed(){
        println("max speed is 220")
    }
}
over 4 years ago · Santiago Trujillo Report

0

You could override color and brand in the constructor of the subclass:

class Toyota(override val color: String = "White", override val brand: String = "White") : Car() {

  override fun speed() {
    println("max speed is 360")
  }

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!