12.What is final class - Using final to prevent inheritance?.

12.What is final class - Using final to prevent inheritance?.

Sometimes you will want to prevent a class from being inherited. To do this, precede the class declaration with final.
Declaring a class as final implicitly declares all of its methods as final, too.
As you might expect, it is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations.

example of a final class:

final class A {

// ...

}

// The following class is illegal.

class B extends A { // erroe! Can't subclass A

}

// As the comments imply, it is illegal for B to inherit A

// since A is declared as final.

 

But, the following class declaration type is not allowed in Java.

public class className extends FinalClass

{

......

}

 

Read More →