114. Prevent Inheritance using 'final' keyword

The second use of 'final' keyword in Inheritance is to prevent inheritance by specifying 'final' for class.

If we specify 'final' for any superclass, then we cant create sub classes for the superclass.

Lets implement this on Eclipse IDE:

1. Create a superclass 'Superclass' by specifying it as 'final' as shown below:

2. Create subclass for the above superclass as shown below:

3. Observe that an error "subclass cannot be created for 'final' specified superclass" is displayed as shown below:

After looking at the error, its very clear that superclass specified as 'final' cannot be inherited by the other sub classes. We've to specify any class as 'final', if and only if we don't want to be inherited by other classes.

Download this project:

Click here to download the project containing the 'Superclass' and 'Subclass' class files used in this post (You can download this project and import into Eclipse IDE on your machine)

Please comment below to feedback or ask questions.

'Packages' concept will be explained in the next post.

No comments:

Followers :)

Blog Index