[SalesForce] override a private abstract method from abstract class

I have an abstract class which has abstract methods. All methods are public. I don't need them to be public, so I tried to remove public or put private. But it keeps saying

Compilation error: AbstractClassHtml: Method does not override an ancestor method: String rr()

 

public abstract class AbstractClassBase { 
  public abstract String tt();
  private abstract String rr();
}

 

public class AbstractClassHtml extends AbstractClassBase {      
  public override String tt() {
    return rr();
  }      
  private override String rr() { // error goes here: Compilation error: AbstractClassHtml: Method does not override an ancestor method: String rr()
    return 'HTML';
  }
}

 
What am I doing wrong?

Best Answer

Unlike Java, private only restricts access to the top-level class. This means that it is possible to override private abstract methods within the same top-level class. Here is an arbitrary example:

public class A {
    public abstract class B {
        private abstract void C();
    }

    public class D extends B {
        private override void C() {
            system.debug('Override private abstract method.');
        }
    }
}

This would be useful for classes that should be extendable, but only give additional functions to classes that are local to the abstract class. For example, a utility class might have many extendable abstract classes that are all derived from a single abstract class, and it uses a private member function to customize the behavior of the other abstract classes.

Related Topic