Skip to content
Related Articles
Open in App
Not now

Related Articles

Java Error – All illegal Modifier Combinations For Methods w.r.t Abstract

Improve Article
Save Article
  • Last Updated : 11 Jan, 2022
Improve Article
Save Article

In Java, we can apply a variety of modifiers to classes, methods, blocks, and variables and each one has a different use case and all of these primarily define how and where those classes, methods, blocks, and variables can be accessed and modified. But not all the modifiers can be applied simultaneously due to some contradiction among their definition and what those modifiers enable us to do. Thus, the combination of modifiers that cannot be applied simultaneously together forms an illegal modifier combination and wherever we use one, we get Compile-time error.

For Example – A method declared as public can be accessed from anywhere in the program but a private method can be accessed only within the class in which it has been declared. Hence, if we try to apply both public and private modifiers to a method it will form an illegal modifier combination and give a compile-time error as shown below.


// Java program to illustrate
// illegal modifier combination
class GFG {
    public static void main(String[] args)
    // the below method has illegal modifier combination
    protected public void m1() {}

The above code throws compile-time error as: error: illegal combination of modifiers: public and protected
   protected public void m1() {}
1 error

Now, before understanding different illegal modifier combinations w.r.t abstract first let’s understand abstract keyword.

In Java, the abstract is a modifier that is applicable for classes and methods but not for variables and it helps us implement Abstraction in Java. Abstraction is a way in which we only expose the services we are offering while hiding the inner details and implementation.

Let’s learn about the abstract method and abstract class:

Abstract Method

Even though we don’t know about implementation still we can write a method with an abstract modifier i.e., the only declaration is available but not implemented. Hence, abstract methods should not have anybody and should end with a semi-colon. For this reason, every class that extends an abstract class must implement all the abstract methods or should be declared as abstract.

An example demonstrating the abstract method is given below:


// Java program to illustrate
// Abstract method
// Importing required packages
import java.util.*;
// Declaring an abstract class
abstract class Animal {
    // Declaring an abstract method
    public abstract String getSound();
// Declaring a class Cat by extending
// the above Animal class
class Cat extends Animal {
    // Implementing the above
    // abstract method of animal class
    public String getSound() { return "Meow!"; }
    // main method
    public static void main(String[] args)
        // Creating an object of type class Cat
        Cat c = new Cat();
        // Calling getSound method



Abstract class

An abstract class is the one for which we can’t instantiate objects and it basically defines and provides a blueprint for derived classes and also methods that the concrete classes must implement while inheriting that abstract class.

Any class that acts as a generalization of classes that inherit it, can be declared as abstract. For example, Animal class is a generalization for all sorts of animals. All animals have some common properties but different values and implementations. So, an Animal class might look like this:

abstract class Animal {

    public abstract String getSound();
    public abstract String getColour();
    public abstract move();
    // Many more methods follow...


Now, before learning all the illegal modifier combinations w.r.t abstract, here’s a quick suggestion:

Note: As abstract never talks about implementation, any modifier that has anything to do with implementation forms an illegal combination with abstract. Also, abstract keyword promotes inheritance and polymorphism and hence, any modifier that restrict inheritance or polymorphism also forms illegal combination. These can be used as thumb-rules while identifying those modifiers forming illegal combination with abstract.

 All illegal Combinations of Modifiers for Methods w.r.t Abstract in Java

The following are various illegal combinations of modifiers for methods w.r.t abstract –

1. Final abstract

  • Compile-time error: Illegal combination of modifiers: ‘final’ and ‘abstract’
  • Reason: Abstract methods must be overridden in a child class to provide implementation whereas final methods cannot be overridden. So, it is an illegal combination.
  • Resolution: We can either declare the method as final only, which will restrict overriding the method in child classes OR we can declare it as abstract only and implement that method in the child classes as required.

2. Native abstract

  • Compile-time error: Illegal combination of modifiers: ‘native’ and ‘abstract’
  • Reason: For native methods implementation is already present in some other languages (C or C++) but for abstract method, an implementation should not be present. So, it forms an illegal combination as well.
  • Resolution: We can use native only if the implementation is already there in other languages OR keep the method as abstract only.

3. Private abstract

  • Compile-time error: Illegal combination of modifiers: ‘private’ and ‘abstract’
  • Reason: abstract methods should be available to the child to provide implementation whereas private methods are not available to child classes, hence it’s an illegal combination.
  • Resolution: If the method is not required in the child classes and is to be used only by the class where it is defined then declare it with private only OR declare it as abstract and implement in child classes.

4. Static abstract

  • Compile-time error: Illegal combination of modifiers: ‘static’ and ‘abstract’
  • Reason: abstract methods must be overridden in a child class to provide implementation whereas static methods cannot be overridden (a static method can be hidden but that is fundamentally different than overriding).
  • Resolution: If you want the method to be implemented by child classes then keep it abstract OR only declare as static if overriding is not to be allowed.

5. Strictfp abstract

  • Compile-time error: Illegal combination of modifiers: ‘strictfp’ and ‘abstract’
  • Reason: If a method is declared as strictfp then it ensures that all floating-point calculations should follow IEEE 754 standard and will give platform-independent results. So basically, it talks about implementation and hence forms an illegal combination with abstract.
  • Resolution: Either declare as abstract only OR declare as strictfp to ensure platform independent floating-point calculations.

6. Synchronized abstract

  • Compile-time error: Illegal combination of modifiers: ‘synchronized’ and ‘abstract’
  • Reason: To prevent simultaneous execution of a method on a given object by multiple threads, the synchronized keyword is used to prevent inconsistency problems. So, when we synchronize a method that implies that we are synchronizing the code in it. As a result, implementation is necessary. Therefore, this combination is also illegal.
  • Resolution: Either declare the method as abstract only OR keep it synchronized if thread safety is important.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!