function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
andresperezandresperez 

Method is not visible (problem with inheritance)

Hi,
 
I am having a problem with inheritance. I am not sure what I am doing wrong... please help.
 
I have an abstract class that looks like this:
public abstract class TokenHandlerAbstract {
  protected Pattern p = null;
 
public Boolean isValid(String testString, TPParams params) {
    Pattern p = getPattern();
    return isPatternMatch(p, testString);
  }
  protected virtual Pattern getPattern() {
    String regexp = functionName + '(.*)';
    if ((p == null) || (p.pattern() != regexp)) {
      p = Pattern.compile(regexp);
    }
    return p;
  }
}
 
This class is extended with a class that looks like this:
public class TokenHandlerString extends TokenHandlerAbstract {
  protected override Pattern getPattern() {
    // Anything surrounded by single quotes
    String regexp = '\'.*\'';
    if ((p == null) || (p.pattern() != regexp)) {
      p = Pattern.compile(regexp);
    }
    return p; 
  }
}

These classes get called from a third class that looks like this:

public class TokenValueFinder {
  private TokenHandlerString thString = new TokenHandlerString();
  public String findValue(String testString, TPParams params) {
    if (thString.isValid(testString, params)) {
      ...
    }
  }
}

When I view the page I get this error:

System.TypeException: Method is not visible: [TokenHandlerString].getPattern()

Class.TokenHandlerAbstract.isValid: line 7, column 21
Class.TokenValueFinder.findValue: line 31, column 20
Class.TPTextParser2.privateParse: line 35, column 31
Class.TPTextParser2.Parse: line 15, column 9
Class.TPResolved2.getTemplateResolved: line 198, column 28
External entry point

If I change the visibility of getPattern() from protected to public, the error goes away. But why?
 
I understand that isValid()has to be public because I am calling the method from a different class. But getPattern() is being called from the same class or one that inherits from it.
 
Thanks
 
Andres Perez
mazizmaziz
This test code works for me:
 
Code:
public abstract class AbstractBaseClass {
  public void publicMethod() {
    System.debug('Public method, calling protected method');
    protectedMethod();
    System.debug('Protected method call worked');
  }
  protected virtual void protectedMethod() {
    System.debug('In base class protected method');
  }
}

public class Subclass extends AbstractBaseClass {
  protected override void protectedMethod() {
    System.debug('In subclass protected method');
  }
}

public class CallingClass {
  private Subclass instance = new Subclass();
  public CallingClass() {
    instance.publicMethod();
  }
}

new CallingClass();

 
This reproduces your case scenario, but not the bug you are encountering. Try executing that (Execute Immediate) to see if you get any error.
 
If you do get an error, then I would surmise that they are not finished rolling out support for protected methods.