Thursday, 20 July 2017

Can you make an abstract class/method final in Java?

No, you cannot make an abstract class or method final in Java because abstract and final are the mutual exclusive concept. 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 as 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 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 modifier with a class is illegal in Java.

Same is true for abstract methods, you cannot make them 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, 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 above points.

Even though it may seem easy that both these concepts are mutually exclusive, many Java progamrmers 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 a common sense once you know the concpet in depth.

Can we make an Abstract class final in Java


Let's try that, the following is a sample code which 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"

If you compile above Java source file using Java compiler (javac) from the command line you will receive a similar error. Eclipse uses slightly different compile but it follows almost all rules of Java compiler. See a Core Java book on core Java to learn more about this concept.

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 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:

Oracle Java Tutorials and Materials, Oracle Java Certifications

You will also receive the error "Cannot override the final method from ImAbstract" on the subclass, as shown in the following screenshot:

Oracle Java Tutorials and Materials, Oracle Java Certifications

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 them final and long answer is what I have explained in this post. Since abstract and final represent two mutually exclusive concepts they cannot be used together.