Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • +1 for interfaces. This is a good example a problem they were designed to solve. Why not more up votes for this answer? It is certainly better than internal flags. Commented Mar 28, 2013 at 3:47
  • 1
    @mortalapeman: "Better" is relative: Using interfaces this way does not make the object immutable but only the mutating methods inaccessible. There is no way to prevent someone having a reference to the object to cast it to the class A and calling e.g. setB(…). With flags (as described in delnans answer) the object ensures that it is immutable. It would probably be a good idea to combine both methods though, i.e. use a flag to ensure immutability and add a interface to let clients only see relevant methods. Commented Apr 19, 2016 at 16:17