Jump to content

Kotlin, is this mean something like dependency injection?

JoneLoveIT
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

 

I dont understand those in red container, is this something related to dependency injection?

image.png.82cec3cb84dd40662a8f26823904b380.png 

 

Link to comment
Share on other sites

Link to post
Share on other sites

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

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×