Difference between Final and Abstract in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// Final class
final class Super {
    private int data = 100;
}
public class Sub extends Super {
    public static void main(String args[])
    {
    }
}

chevron_right


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// an abstract class
  
// Abstract parent class
abstract class Book {
  
    // Abstract method without body
    public abstract void page();
}
  
// shayar class extends Book class
public 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();
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

final class A {
    public abstract void methodOne();
}

chevron_right


Clearly, this implementation is invalid because a final class cannot have an abstract method. As a final class cannot be inherited.

filter_none

edit
close

play_arrow

link
brightness_4
code

abstract class A {
    public final void methodOne() {}
}

chevron_right


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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.