From c6d114bf5b0606637e6b2ebdfc0d48a276ed14fc Mon Sep 17 00:00:00 2001 From: Wojciech Nagrodzki <278594+wnagrodzki@users.noreply.github.com> Date: Sat, 4 Aug 2018 19:54:57 +0200 Subject: [PATCH] Added "Controlling subclassing behavior" section --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 35c7eb1..80e7c56 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,13 @@ If a method is independent of instance state it should be declared as type method, preferably by using `static` keyword (or `class` in case when the method is designed to be overridden in a subclass). > This is to avoid methods giving a false impression that they are dependent on the instance state while in fact they are not. + +### Controlling subclassing behavior + +Subclassing behavior should be designed with maximal hermetization principle in mind - choosing most restrictive access level possible. + +Declare class `final` unless you explicitly design it to be inheritable. For non final class declare all instance members as `final` and type members as `static` except for those you explicitly design for overriding. + +When designing a module prefer `public` for non final class unless you explicitly design it to be inheritable by external clients. For `open` class prefer `public` for members except for those you explicitly design for overriding by external clients. + +> Every point that can be overridden increases complexity, thus it should always be a conscious choice to add it.