开发者

Abstract classes, why can't we declare private val and var class member?

abstract class Table {
  private val records: Int
}

Is it because we have t开发者_如何学Goo create an instance of an abstract class before we can access its private member?


To extend a bit on @Owen's answer: you can declare private members.

abstract class Table {
  private val records: Int = 0
}

But you can't declare abstract private members. Why? Because any concrete class which extends an abstract class must override any abstract members, and it can't override a private member. So you couldn't have any concrete classes which extend Table at all.


I would imagine this is because there is no way to make them concrete:

class Foo extends Table {
    override val records = 3
}

would fail, because records is private to Table.

It would make Table kind of useless. I can't see that it would hurt anything, just it almost certainly indicates a mistake by the programmer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜