Difference between Final and Abstract in Java - GeeksforGeeks

07 May, 2020
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.
// Java program to demonstrate the// Final classfinal class Super {    private int data = 100;}public class Sub extends Super {    public static void main(String args[])    {    }}
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.
// Java program to demonstrate// an abstract class  // Abstract parent classabstract class Book {      // Abstract method without body    public abstract void page();}  // shayar class extends Book classpublic class shayar extends Book {      // Declaring the abstract method    public void page()    {        System.out.println("Geek");    }      // Driver code    public static void main(String args[])    {        Book obj = new shayar();        obj.page();    }}
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.
final class A {    public abstract void methodOne();}
Clearly, this implementation is invalid because a final class cannot have an abstract method. As a final class cannot be inherited.
abstract class A {    public final void methodOne() {}}
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:
java-basics
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Exceptions in Java
StringBuilder Class in Java with Examples
Comparator Interface in Java with Examples
Generics in Java
Functional Interfaces in Java
Java Programming Examples
HashMap get() Method in Java