Private abstract method in Java

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

Please follow and like us:
Content Protection by DMCA.com