Error class is final in java

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.

Читайте также:  Шахматы на python код

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:

Читайте также:  Асинхронный запрос php это

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 you make an abstract class/method final in Java?

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:

Can an abstract class be final in Java

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.

Is it possible to make an abstract method final in Java

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.

Источник

Why can’t a Java class be both abstract and final?

A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.

If you want to use the concrete method in an abstract class you need to inherit the class, provide implementation to the abstract methods (if any) and then, you using the subclass object you can invoke the required methods.

Example

In the following Java example, the abstract class MyClass contains a concrete method with name display.

From another class (AbstractClassExample) we are inheriting the class MyClass and invoking the its concrete method display using the subclass object.

abstract class MyClass < public void display() < System.out.println("This is a method of abstract class"); >> public class AbstractClassExample extends MyClass < public static void main(String args[]) < new AbstractClassExample().display(); >>

Output

This is a method of abstract class

Final class

If you declare a class final you cannot extend if you try to extend a final class, a compile time error will be generated saying “cannot inherit from final SuperClass”

Example

In the following Java program, we have a final class with name SuperClass and we are trying to inherent it from another class (SubClass).

Compile time error

On compiling, this program generates a compilation error as shown below −

SubClass.java:7: error: cannot inherit from final SuperClass public class SubClass extends SuperClass< ^ 1 error

Both abstract and final

If you declare a class abstract, to use it, you must extend it and if you declare a class final you cannot extend it, since both contradict with each other you cannot declare a class both abstract and final if you do so a compile time error will be generated.

Example

public final abstract class Example < public void sample() < >>

Compile time error

Example.java:1: error: illegal combination of modifiers: abstract and final public final abstract class Example < ^ 1 error

Источник

Оцените статью