How protected method in scala works on jvm

I am new to Scala. I read that the protectedScala keyword is different from protectedin Java. When I saw the byte code generated for the protected public class method in Scala and java, I found the following:

Scala code:

package com.test 
class Vehicle {
  protected def ignite() {
    println("Ignition.....")
  }
}

when I decompiled with javap, it shows the following code:

public class com.test.Vehicle {
  public void ignite();
  public com.test.Vehicle();
}

And also flags: ACC_PUBLICset in the method descriptor ignitefor Scala.

Java equivalent code:

package com.test;
public class Vehicle {
  protected void ignite() {
    System.out.println("Ignition.....");
  }
}

and compiled code:

public class com.test.Vehicle {
  public com.test.Vehicle();
  protected void ignite();
}

It is also flags: ACC_PROTECTEDset in the method descriptor ignitefor Java.

But still it gives a different behavior than JAVA.

How is this thing handled by the JVM?

Note. I do not have knowledge of the JVM specification.

+4
2

Scala protected ( , - JVM), JVM; Scala Scala, ( public JVM).

+2

@Alexey answerd, . JVM . JVM .

:

package Protected {
  package test {
    class Vehicle {
      protected def checkEngine() {}

      private[Protected] def ignite() {}

      private[test] def injectFuel() {}

      private[this] def openInletValve() {}

      def start() {}
    }
  }
}

javap

public class Protected.test.Vehicle {
  public void checkEngine();
  public void ignite();
  public void injectFuel();
  public void start();
  public Protected.test.Vehicle();
}
0

All Articles