abstract class Table {
private val records: Int
}
Is it because we have to create an instance of an abstract class before we can access its private member?
Abstract classes can have private methods. Interfaces can't. Abstract classes can have instance variables (these are inherited by child classes).
But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use. Therefore, the abstract method cannot be private.
The main purpose of the constructor is to initialize the newly created object. In abstract class, we have an instance variable, abstract methods, and non-abstract methods. We need to initialize the non-abstract methods and instance variables, therefore abstract classes have a constructor.
An abstract method cannot be private as in the following, abstract class Demo() { private abstract void Call();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With