![]() |
VOOZH | about |
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.
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:
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:
Meow!
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.
The following are various illegal combinations of modifiers for methods w.r.t abstract -