Open In App

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

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
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
 
        System.out.println("GFG!");
    }
 
    // the below method has illegal modifier combination
    protected public void m1() {}
}

The above code throws compile-time error as:

GFG.java:13: 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
        System.out.println(c.getSound());
    }
}

Output
Meow!

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

2. Native abstract

3. Private abstract

4. Static abstract

5. Strictfp abstract

6. Synchronized abstract


Article Tags :