Final class

Class Design Indicator

Created by bpch.vector - Freepik.com

Disclaimer : The content of this blog are my views and understanding of the topic. I do not intend to demean anything or anyone. I am only trying to share my views on the topic so that you will get a different thought process and angle to look at this topic.

Introduction

A final class denotes that Design is finalised and cannot be extended further. Having final classes for a finalised design will benefit in clearly communicating other developers but will it impact the extensibility principle ? What are the cases which makes a class a potential candidate for finalisation ? Are we not adhering to the Design principles if we make the classes final ? What is need to clear communication among Developers ?

Language is a means of communication and Programming Languages help two kinds of communication : Communication between Humans & Machines and between Humans.

A program is a communication between humans and machines

We are trying to explain the machine what we intend it to do. We are in the current age when we are at a higher level of abstraction. Now we don't do the assembly language level of programming and we are trying to surpass this and make machines self learning and understanding based on the Data. Still the kind of program may change and become more abstract but we have to have some way to communicate to machines.

A program is also a communication between humans (Software Developers)

A program written by one Software Developer should be understood by another. Many argue over this statement saying that if both the Software Developers understand the programming language then it should not be difficult for a Developer to understand what a program does ? I don't agree to this point. Take an example below and let me know if you could read below sentence.

Thissent enceisan exam pleofwh atabad pro graml ooksl ike

You might have found it difficult reading this sentence but later you were able to understand that it says :

This sentence is an example of what a bad program looks like

Whats wrong with the sentence. We all know English language. We all know it has letter and words. The sentence above uses the same english alphabets. It has some words which are present in the sentence. Only thing is the spaces are not properly given between words. Its how a bad program looks like its difficult to read quickly then a Developer has to spend hours debugging when there is an issue or trying to understand what is being coded.

Now let's come back to the topic. A final class has a final keyword which can show that it's not be extended or moreover it cannot be extended. Its mostly used for the cases where a library owner does not want the consumer of the library to extend it and create its extensions which may lead to un-predictable behaviour. What are some of the examples of these kind of classes ? I will give a real life example for this then we will try to find such examples with some correlation.

The final keyword restricts extension or inheritance. In real life we know that we cannot have any child class of Colour class. This is applicable if the inheritance criteria is based on the wavelengths of light. Red coloured light has a wavelength of ~ 700–635 nm and Yellow has ~ 635–590 nm (where nm - nanometers). In case of the libraries lets say you have created a parser which can parse XML, JSON and CSV files. You have the library designed for supporting these file formats and dedicated classes are there for them. You don't want the consumer of the library to extend these classes as it may create some undesired results. Hence you will make them final. Simply put, you will make all those classes final which you don't want the consumer of the library to extend. You will let those classes non-final which support extension. In a way you are in control of the extensions supported by your library.

A final class can also be created in the same project when we realise the fact that it should not be extended further. Possible scenarios are :

Summary

A final class is an indication that its the least child in the hierarchy and its design is final and could not be extended. Hence we have to be cautious while creating final classes but we should not be hesitant to make a class final.