In this article, the difference between the abstract class and the final class is discussed. Before getting into the differences, lets first understand what each of this means.
Final Class: A class which is declared with the “Final” keyword is known as the final class. The final keyword is used to finalize the implementations of the classes, the methods and the variables used in this class. The main purpose of using a final class is to prevent the class from being inherited (i.e.) if a class is marked as final, then no other class can inherit any properties or methods from the final class. If the final class is extended, Java gives a compile-time error.
The following is an example of how a final class is declared. However, a compile-time error is given because this final class is being inherited.
Abstract Class: A class that is declared using the “abstract” keyword is known as an abstract class. The main idea behind an abstract class is to implement the concept of Abstraction. An abstract class can have both abstract methods(methods without body) as well as the concrete methods(regular methods with the body). However, a normal class(non-abstract class) cannot have abstract methods.
The following is an example of how an abstract class is declared.
The above program gives “Geek” as the output. All the abstract methods should be overridden in the child class to provide the implementation. However, from the definition, a final method can’t be overridden. Therefore, an abstract final combination is illegal for the methods. And also, for the abstract classes, we need to create a child class to provide the implementation whereas for final class we can’t create child class. therefore, a final abstract combination is illegal for classes. Hence, a final class cannot contain abstract methods whereas an abstract class can contain a final method.
Below is an example which demonstrates the combination of abstract and final classes.
Clearly, this implementation is invalid because a final class cannot have an abstract method. As a final class cannot be inherited.
However, an abstract class can have a final method. This final method is treated like a normal method with a body which cannot be overridden.
The following table demonstrates the difference between an abstract class and a final class:
|S.No.||ABSTRACT CLASS||FINAL CLASS|
|1.||Uses the “abstract” key word.||Uses the “final” key word.|
|2.||This helps to achieve abstraction.||This helps to restrict other classes from accessing its properties and methods.|
|3.||For later use, all the abstract methods should be overridden||Overriding concept does not arise as final class cannot be inherited|
|4.||A few methods can be implemented and a few cannot||All methods should have implementation|
|5.||Cannot create immutable objects (infact, no objects can be created)||Immutable objects can be created (eg. String class)|
|6.||Abstract class methods functionality can be altered in subclass||Final class methods should be used as it is by other classes|
|7.||Can be inherited||Cannot be inherited|
|8.||Cannot be instantiated||Can be instantiated|
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.