Deconstructor can not be overriden ? Was: Deconstruction patterns

Brian Goetz brian.goetz at oracle.com
Tue Mar 7 18:02:56 UTC 2023


> Conceptually that violate the principle of encapsulation, you can access to the state of B without using a method of B.

No, it is an application of LSP; if a B is-a A, I should be able to use 
a B wherever I currently use an A.  B doesn't necessarily get to 
interpose itself everywhere; it can't interpose against instanceof 
testing for A, or casting to A, and it can't override final methods from 
A.  That's what "extends" means.

> A de-constructor is also a bundle of accessors, like if you were able to call all the accessors at once.
> Accessors are overridable for a good reason, otherwise you can leak the internal state.

Consider A's dtor as a set of final accessors that B cannot override.  
It can provide other accessors in addition.

If you have a point here, I'm not seeing it?


More information about the amber-spec-observers mailing list