Home > database >  Scala overriding Java fields and members
Scala overriding Java fields and members

Time:11-01

I'm trying to extend the class A which is written in Java to class B in Scala.

class A { 
    private Pattern pattern;
    private String regex= "folder1/folder2/folder3/.*";
    A(...){
       this.regex = regex;
       this.pattern = Pattern.compile(getRegex());
    }
    
    
    public String getRegex() {
        return regex;
    }
}

class B(...) extends A(...) {
    val regex: String= "folder4/.*";
    
    override def getRegex(): String = {
        return regex;
    }
}

However it seems that the Pattern.compile(getRegex()) is getting null value from the B class. I'm also not allowed to pass the override regex through the constructor. Not sure how I can resolve this issue.

CodePudding user response:

This has nothing to do with scala specifically.

The regex instance variable you create in class B is initialized at some point. Unfortunately, that point is after A's constructor runs.

In general, when a constuctor in Parent invokes an instance method that is overridden in Child, all heck breaks loose. Doesn't matter if it's all java, all scala, or a combination of the two.

There are a few solutions. The simplest is to simply get rid of that field. If the getRegex() method instead is written as return "folder4/.*" instead of return regex, this problem won't occur.

CodePudding user response:

See Java order of Initialization and Instantiation

The constructor is executed at the step 3

  1. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

And an instance field is initialized at the step 4

  1. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.

So at the step 3 in the line this.pattern = Pattern.compile(getRegex()) inside the constructor of A, getRegex() returns regex, which is null at the moment (regex will become "folder1/folder2/folder3/.*" later, at the step 4).

  • Related