- Difference between Final and Abstract in Java
- Java
- Java
- Java
- Java
- Can you make an Abstract Class or Method Final in Java? Example
- Can an Abstract class be Final in Java?
- Can we make an Abstract method final in Java?
- can abstract class be final in java?
- Java interview questions on interface and abstract class
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.
Java
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
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.
Java
Clearly, this implementation is invalid because a final class cannot have an abstract method. As a final class cannot be inherited.
Java
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 |
9. | Abstract class may have final methods. | Final class does not have abstract methods or final methods. |
10. | Abstract class helps in to achieve Abstraction. | Final class can help to restrict the other classes from accessing the properties and methods. |
Can you make an Abstract Class or Method Final in Java? Example
No, you cannot make an abstract class or method final in Java because the abstract and final are mutually exclusive concepts. An abstract class is incomplete and can only be instantiated by extending a concrete class and implementing all abstract methods, while a final class is considered complete and cannot be extended further. This means when you make an abstract class final, it cannot be extended hence it cannot be used and that’s why the Java compiler throws a compile-time error when you try to make an abstract class final in Java. In short, an abstract class cannot be final in Java, using both abstract and final modifiers with a class is illegal in Java.
This is also one of the most popular Java interview questions, if you are preparing for Java interviews, knowing this concept can help you do well on your interviews.
The same is true for abstract methods, you cannot make the final in Java. An abstract method must be overridden to be useful and called but when you make the abstract method final it cannot be overridden in Java, hence there would be no way to use that method.
This is why making an abstract method final in Java will result in a compile-time error. In short, the use of keywords abstract and final together is illegal in Java. You can verify these facts by yourself by trying to make an abstract class/method final in Java. In this article, I’ll show you an example of both to prove the above points.
Even though it may seem easy that both these concepts are mutually exclusive, many Java programmers get confused between them. They just don’t understand that abstract means incomplete and final means complete, and how a thing can be both complete and incomplete at the same time? It’s just common sense once you know the concept in depth.
Btw, if you are new to the Java world or self-learning Java then I suggest you also join The Complete Java Masterclass on Udemy. One of the most comprehensive and up-to-date courses in Java. It is recently updated for Java 12 and I highly recommend to all my readers to learn Java or fill gaps in their learning.
Can an Abstract class be Final in Java?
There is a proverb in Hindi, «Haath Kangan ko Aarshi Kya, Padhe Likhe ko Parsi kya«, which means instead of saying just show. So, why not we instead of saying that whether we can make an abstract final in Java or not, just code it and see what happens.
Let’s try that, the following is a sample code that uses both abstract and final modifier together at class declarations when you write this code in Eclipse, the IDE will suggest this is wrong by giving an error as shown below:
abstract final class ImAnAbstractClass
Error: «The class ImAnAbstractClass can be either abstract or final, not both»
Unlike many others, this error is both clear and concise and shows that it’s not possible to make a class both final and abstract at the same time in Java.
Btw, If you compile the above Java source file using Java compiler (javac) from the command line you will receive a similar error. Eclipse uses a slightly different compile but it follows almost all rules of the Java compiler.
If you want to learn more about what rules Java compiler follows or more of such fundamental concepts, you can also join Java Fundamentals Part 1 and Part 2 courses on Pluralsight, two of the best courses to learn Java from scratch.
Can we make an Abstract method final in Java?
Now that, you know there is no way you can make an abstract class final in Java, let’s try to make an abstract method final in Java. As I said above, this time also Java compiler should complain because both final and abstract are mutual exclusive keywords:
abstract class ImAbstract< public final abstract void anAbstractMethod(); > class ImConcrete extends ImAbstract< @Override public void anAbstractMethod() < // TODO Auto-generated method stub > >
This will give the error «The abstract method anAbstractMethod in type ImAbstract can only set a visibility modifier, one of public or protected» when you write this code in Eclipse, as shown in the following screenshot:
You will also receive the error «Cannot override the final method from an abstract» on the subclass, as shown in the following screenshot which shows cannot override the final method from an abstract class.
This makes it clear that it’s not possible to make a class both abstract and final in Java. It’s one of the fundamental concepts and If you want to learn more about Java fundamentals, I suggest you read a good core Java book like Core Java For the Impatient by Cay. S. Horstmann.
That’s all about whether you can make an abstract class/method final in Java or not. The short answer is no, you cannot make the final and the long answer is what I have explained in this post. Since abstract and final represent two mutually exclusive concepts they cannot be used together.
Some of you might be wondering about variables? Can we make a final variable abstract in Java? Well, a variable cannot be abstract in Java. The use of abstract keywords is illegal for a variable, it can only be used with class or method, hence there is a way you can make a final variable abstract in Java.
- Can abstract class have a constructor in Java? (answer)
- Is it possible to instantiate an Abstract class in Java? (answer)
- Review these 50 Java interview questions before the Interview (questions)
- Can you override a static method in Java? (answer)
- 10 Courses to Crack any Java Programming interview (best courses)
- Can you overload a static method in Java? (answer)
- 10 Data Structure and algorithms courses for coding interviews (online courses)
- Is it possible to create an Abstract method in the final class? (answer)
- Can you override a private method in Java? (answer)
- Top 5 Websites to learn Java Coding for FREE (best websites)
- Can you overload and override the main() method in Java? (answer)
- Can you make an Array volatile in Java? (answer)
- Can you declare a Class static in Java? (answer)
- 50+ Data Structure and Algorithms Interview Questions? (questions)
- Must-Read Books for Java Programmers (books)
- Some Free Online courses to learn Java 8 and 9 features? (courses)
- 10 Frameworks Java Programmer Should Learn? (courses)
- 5 Free Data Structure and Algorithms Courses for Beginners (courses)
P. S. — If you are self-learning Java and looking for a project-based free course to start your journey then you should check out these free Core Java courses on Udemy. It’s completely free, all you need is to create an Udemy account to access this course.
can abstract class be final in java?
No, abstract class can’t be final in Java because abstract classes are used only by extending and if they made final they can’t extended.
final abstract class DisplayTest { abstract void display(String str); } public class Main extends DisplayTest { public void display(String str) { System.out.println("Hello " + str); } public static void main(String[] args) { DisplayTest displayTest = new Main(); displayTest.display("w3spoint.com"); } }
Main.java:1: error: illegal combination of modifiers: abstract and final final abstract class DisplayTest { ^ Main.java:7: error: cannot inherit from final DisplayTest public class Main extends DisplayTest ^ 2 errors
Main.java:1: error: illegal combination of modifiers: abstract and final final abstract class DisplayTest < ^ Main.java:7: error: cannot inherit from final DisplayTest public class Main extends DisplayTest ^ 2 errors