- Trending Categories
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
- Related Articles
- Can we declare an abstract method, private, protected, public or default in java?
- Can constructors be marked final, abstract or static in Java?
- Can we declare the method of an Interface final in java?
- Can we declare the main () method as final in Java?
- Can we declare an interface as final in java?
- Can We declare main() method as Non-Static in java?
- Can we declare a static variable within a method in java?
- Can we declare constructor as final in java?
- Declare static variables and methods in an abstract class in Java
- Can we declare final variables without initialization in java?
- Can we define an abstract class without abstract method in java?\n\n
- Why can't static method be abstract in Java?
- Can we override a private or static method in Java
- Can we overload or override a static method in Java?\n
- Can we have a private method or private static method in an interface in Java 9?\n
