A friend of mine was asked that question in his on-phone job interview a couple of days a go. I don't have a clue. can anyone suggest a solution? (His job interview is over. just out of curiosity now ) 10x.
You can make your class immutable without using final keyword as: Make instance variable as private. Make constructor private. Create a factory method which will return the instance of this class.
A class can be made final by using the final keyword. The final class cannot be inherited and so the final keyword is commonly used with a class to prevent inheritance.
Hence, a final class cannot contain abstract methods whereas an abstract class can contain a final method.
The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With