- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we declare an abstract method final or static in java?
A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.
public abstract myMethod();
To use an abstract method, you need to inherit it by extending its class and provide implementation to it.
Declaring abstract method static
If you declare a method in a class abstract to use it, you must override this method in the subclass. But, overriding is not possible with static methods. Therefore, an abstract method cannot be static.
If you still, try to declare an abstract method static a compile time error is generated saying “illegal combination of modifiers − abstract and static”.
Example
In the following Java program, we are trying to declare an abstract method static.
abstract class MyClass { public static abstract void display(); } public class AbstractClassExample extends MyClass{ public void display() { System.out.println("This is the subclass implementation of the display method "); } public static void main(String args[]) { new AbstractClassExample().display(); } }
Compile time error
On compiling, the above program generates the following error.
AbstractClassExample.java:2: error: illegal combination of modifiers: abstract and static public static abstract void display(); ^ 1 error
Declaring abstract method final
Similarly, you cannot override final methods in Java.
But, in-case of abstract, you must override an abstract method to use it.
Therefore, you cannot use abstract and final together before a method.
If, you still try to declare an abstract method final a compile time error is generated saying “illegal combination of modifiers: abstract and final”.
Example
In the following Java program, we are trying to declare an abstract method final.
public abstract class AbstractClassExample { public final abstract void display(); }
Compile time error
On compiling, the above program generates the following error.
AbstractClassExample.java:2: error: illegal combination of modifiers: abstract and final public final abstract void display(); ^ 1 error