Can an abstract class be final in java?

Before reading this topic I will recommend you to read the abstract class in Java. Now we have good knowledge of the abstract class. But there are some concepts which are hidden from us. Let’s discuss one of them. Many programmers have a question, can an abstract class be final in java?
No, An abstract class can’t be final because the final and abstract are opposite terms in JAVA.
Reason: An abstract class must be inherited by any derived class because a derived class is responsible to provide the implementation of abstract methods of an abstract class. But on another hand, if a class is a final class, then it can’t be extended(inherited). So both concepts are opposite to each other.

Can abstract class be final in java
abstract final class Department
{
	String collegeName = "KUK";
	public void collegeName()
	{
		System.out.println("Name of college = "+ collegeName);
	}
	//abstract methods
	public abstract void deptName();
	public abstract void noOfTeachers();
	
}



class MainClass extends Department
{
	public static void main(String arg[])
	{
		MainClass obj = new MainClass();
	}

	@Override
	public void deptName() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void noOfTeachers() {
		// TODO Auto-generated method stub
		
	}
}

Output: This will show exception at compilation time.

As you can see in the above example, We are trying to use two opposite keywords (abstract and final) for a single class. In this case, the compiler showing an error compile-time, because Java doesn’t permit it.

Because if compiler works according to the final keyword then the Department class must not be extended by any other class. But the abstract keyword is the vice-versa. It means according to abstract keyword the Department must be extended by some other class. To remove this ambiguity of compiler, Java doesn’t permit it.

Leave a Comment