1.

Restrict Inheritance for a class in Java

Answer»

Inheritance can be restricted for a class in Java using the KEYWORD final. In other WORDS, if a class is declared as final, then no other class can be extended from it. This is quite useful while creating an immutable class  

An example of this is given as follows:

final class C1 {     // methods and fields } class C2 extends C1 {     // Not possible }

An error is generated if the class C2 tries to EXTEND the class C1 as it is a final class and so cannot be inherited.



Discussion

No Comment Found