Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between Final and Abstract in Java

  • Difficulty Level : Easy
  • Last Updated : 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.

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.

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 class
final 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 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();
    }
}

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:

S.No.ABSTRACT CLASSFINAL 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 overriddenOverriding concept does not arise as final class cannot be inherited
4.A few methods can be implemented and a few cannotAll 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 subclassFinal class methods should be used as it is by other classes
7.Can be inheritedCannot be inherited
8.Cannot be instantiatedCan be instantiated



My Personal Notes arrow_drop_up

Start Your Coding Journey Now!