Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Class invariant
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Class invariants and inheritance== The useful effect of class invariants in object-oriented software is enhanced in the presence of inheritance. Class invariants are inherited, that is, "the invariants of all the parents of a class apply to the class itself."<ref>Meyer, Bertrand. ''[[Object-Oriented Software Construction]], second edition,'' Prentice Hall, 1997, p. 570.</ref> Inheritance can allow descendant classes to alter implementation data of parent classes, so it would be possible for a descendant class to change the state of instances in a way that made them invalid from the viewpoint of the parent class. The concern for this type of misbehaving descendant is one reason object-oriented software designers give for favoring [[composition over inheritance]] (i.e., inheritance breaks encapsulation).<ref>E. Gamma, R. Helm, R. Johnson, and J. Vlissides. ''[[Design Patterns: Elements of Reusable Object-Oriented Software]].'' Addison-Wesley, Reading, Massachusetts, 1995., p. 20.</ref> However, because class invariants are inherited, the class invariant for any particular class consists of any invariant assertions coded immediately on that class [[Logical conjunction|in conjunction with]] all the invariant clauses inherited from the class's parents. This means that even though descendant classes may have access to the implementation data of their parents, the class invariant can prevent them from manipulating those data in any way that produces an invalid instance at runtime.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)