Kotlin, is this mean something like dependency injection?
Go to solution
Solved by Eigenvektor,
This has nothing at all to do with dependency injection.
It just says you shouldn't define your class like this:
open class Dwelling(val residents: Int) class SquareCabin(val residents: Int) : Dwelling(residents) // ^ Don't do this! // --> 'residents' hides member of supertype 'Dwelling' and needs 'override' modifier
because residents actually belongs to the parent class (Dwelling) and declaring it as a val would shadow the variable declared by your super class. The comment I placed beneath is what IntelliJ shows you as a warning if you try.
So it's telling you, you don't want to re-declare the field, you just want to pass the value to the parent class, hence you shouldn't use "val" in the SquareCabin. That's all there is to it. So your class should simply be declared like this:
open class Dwelling(val residents: Int) // ^ could also use var here, if it should be a mutable property of the class class SquareCabin(residents: Int) : Dwelling(residents) // ^ no val/var here, we're not declaring a property, // just a constructor argument that is passed the parent class
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now