public/protected/private is an unnecessary feature Apologies for the unoriginal OOP example, but: Suppose class Car implements interface Vehicle. Regular code using this interface works great: Users can write generic code that works for any Vehicle, and use a Car with that code. The implementer of Car can restrict users to only ever use Car instances through the Vehicle interface, by only allowing construction of Car instances through, for example, a function make_car with return type Vehicle. But the interface doesn't work with inheritance: Users cannot write a generic class that can inherit from any Vehicle (without the addition of more complex features). The implementer of Car cannot restrict subclasses of Car to only use Car through the Vehicle interface. We'll ignore point 3, and focus on point 4. Since Car can't restrict subclasses to only use an interface, any subclasses can violate Car's internal invariants. So either Car should disallow inheritance (always an option by simply not exposing the Car type at all, as in point 2), or Car needs another way to define an interface that will take effect on subclasses. That other way to define an interface which works for subclasses is the access modifiers: public, protected, and private. By marking members as private, Car can do something like define an interface which affects subclasses; this pseudo-interface contains exactly and only those members of Car which are public and protected. But this is absurd: Why do we have two ways to define an interface? Wouldn't it be better for Car to be able to force subclasses to only use Car through a specific (normal) interface? There'd be no loss in functionality; Car could still have separate interfaces for instantiators and subclasses. We'd just define those interfaces with the same mechanism, instead of using one mechanism for instantiators and another for subclasses. Access modifiers were originally invented in Simula. As far as I can tell from extensive research, the inve...
First seen: 2025-06-19 18:09
Last seen: 2025-06-20 01:21