Can you declare abstract class final
Answer: No the class can be either abstract or final, not both.
Let’s think why somebody asked this questions what was in his mind or just he was testing java knowledge?
Look below if somebody want to do this:
package com.javahonk; final abstract class AbastractClassTest { public void method1() { } // More private methods and fields... }
Solution 1: Create final class with private constructor means no class instantiate it and it can’t be sub-classed. ( Singleton design pattern indeed)
package com.javahonk; final class AbastractClassTest { //private default constructor ==> can't be instantiated //side effect: class is final because it can't be subclassed: //super() can't be called from subclasses private AbastractClassTest() { throw new AssertionError(); } public static void doSomething() {} }
Solution 2: You can’t get much simpler than using an enum with no instances.
package com.javahonk; public enum EnumTest { myTestMethod() { } }
enum class is final, with explicitly no instances.
This is detected by the compiler rather than as a runtime error. (like throwing an exception)