Private methods are private to the class only. They are not polymorphic in nature i.e. we cannot inherit them, so it makes no sense to make a private method abstract. Making a method abstract means we have to override and implement it in a subclass, but since we can’t override private methods, we can’t make them abstract either.
Example:
abstract class Display { private abstract void display(); } public class Main extends Display { void display() { System.out.println("Inside display method"); } public static void main(String args[]) { System.out.println("Inside main class"); } } |
Output:
Main.java:9: error: illegal combination of modifiers: abstract and private private abstract void display(); ^ Main.java:12: error: Main is not abstract and does not override abstract method display() in Display public class Main extends Display { ^ 2 errors |
Java interview questions on access modifiers
- what are access modifiers in java?
- What are different types of access modifiers in java?
- What are non access modifiers in java?
- Can we use abstract and final both with a method?
- Can abstract class have final methods in java?
- Can we declare a class as private in java?
- Can we declare an abstract method as private?
- Can we declare a class as protected in java?