Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between protected and protected[this]

I have the following code:

class Base{
    protected val alpha ="Alpha";
    protected def sayHello = "Hello";
}
class Derived extends Base{
        val base = new Base;
        def hello = println(this.alpha +" "+this.sayHello) ;
       // def hello = println(base.alpha +" "+base.sayHello) ; // don't compile
    }

    object MyObj extends App{
            val x=new Derived;
            x.hello;
        }

In class Base, if I label protected with this, the code works as expected; if I don't label it with this, everything works as expected too.

Are protected and protected[this] equivalent in Scala? If yes, why would Scala provide both? If not, how do they differ?

like image 712
optional Avatar asked Apr 28 '16 07:04

optional


People also ask

What is the difference between protected and protected internal?

protected: The type or member can be accessed only by code in the same class , or in a class that is derived from that class . internal: The type or member can be accessed by any code in the same assembly, but not from another assembly.

What is protected internal?

The protected internal keyword combination is a member access modifier. A protected internal member is accessible from the current assembly or from types that are derived from the containing class. For a comparison of protected internal with the other access modifiers, see Accessibility Levels.

What is protected C#?

Protected is a keyword that C# uses to make access restriction for class members. When we mark members as protected, it becomes accessible only in the class where it's defined or inside the derived class. The protected keyword is used to share functionality that derived classes might find useful.

Is protected and private the same?

The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.


1 Answers

Scala protected and its siblings protected[this] , protected[pkg] is somewhat overwhelming ,However I found easy to remember solution by using the Java philosophy of protected . 1st How protected member is visible to in Java

  1. They are visible to subclass(subclass may be in same package or other package)
  2. They are visible to any class that is in same package as class that specified protected member.

Obviously they are visible to class itself.

But Scala has some restriction on how they are visible to subclass.By default,They are visible to subclass only . They are not visible to package in which class is declared .However two case exist how they are visible in subclass .

if protected member is not qualified(plain protected) then it is visible with another instances of declaring class into declaring class as well as with this into class and subclass e.g

class Base{
    protected val alpha ="Alpha";
    protected def sayHello = "Hello";
}
class Derived extends Base{
        def hello = println((new Derived()).sayHello) ;
        def hello2 = println(this.sayHello);

}

if protected member is qualified with this .It is only accessible with this in class and subclass ,It can't be accessed by other instances of declaring class or subclass e.g.

class Base{
    protected val alpha ="Alpha";
    protected[this] def sayHello = "Hello";
    def foo = Console println(new Base().sayHello) // won't compile
    def bar = Console println(this.sayHello)
}
class Derived extends Base{
        def hello = println(this.sayHello) ;
        //def hello2 = println((new Derived() .sayHello) // won't compile
}

Since Scala do not support the package level access of protected member by default .but if you want to make it available at package level then you need to specify package explicitly e.g. protected[pkg]. Now this protected member is visible with declaring class/Subclass instances if they are accessed in classes declared in pkg or below.

e.g.

package com.test.alpha{
class Base{
    protected val alpha ="Alpha";
    protected[test] def sayHello = "Hello"; // if you remove [test] it won't compile
}

class Derived extends Base{
    val base = new Base
        def hello = println(base.sayHello) ;
}}

This is how one can remember Scala protected

like image 176
optional Avatar answered Sep 30 '22 01:09

optional