JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

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



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,

.