4

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?

2 Answers 2

5

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.

7

You can't override private methods (otherwise, its not very private), you should be able to override a protected or public method.

Private: This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If you do not specify an access modifier, the method or variable is private.

Protected : This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.

Public :This means the method or variable can be used by any Apex in this application or namespace.

4
  • I see now. I got the concept. Thanks! But it raise another question - why the heck it allows me to save private abstract ??? Commented Dec 4, 2014 at 17:09
  • Since an abstract class can contain functionality .. it can have private variables or methods.See this link for more details stackoverflow.com/questions/3334380/…
    – sfdc
    Commented Dec 4, 2014 at 17:33
  • I'm totally ok with private methods. But private abstract??? Commented Dec 4, 2014 at 19:11
  • @Chiz You've accepted the wrong answer... There is actually a reason.
    – sfdcfox
    Commented Dec 5, 2014 at 2:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .