top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Explain the difference between Scala’s protected and Java’s protected Access Modifier?

+1 vote
453 views
Explain the difference between Scala’s protected and Java’s protected Access Modifier?
posted Aug 11, 2016 by Karthick.c

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In Java, we can use ‘protected’ keyword to access that component in the same package (within all sub-classes or any other classes which are available in the same package) or any subclass available in any package.
In Scala, ‘protected’ access modifier has different meaning. We can use it in two different ways:

protected
protected[T]

Here “T” parameter can be a Scaa class/trait, package or object.
If we use Non-Parameterized protected that is “protected”, then that is visible only from it’s subclasses. It not visible from the same package or outside.
If we use Parameterized protected that is “protected[T]”, then that is visible from that component only.

Examples:

If we use something like protected[Account] where “Account” is a Scala class, then that is visible within that class only. It is not visisble outside that class.
If we use something like protected[retailbank] where “retailbank” is a Scala package, then that is visible within that package (and sub-packages) only. It is not visible outside that package.

answer Sep 15, 2016 by Dominic
Similar Questions
+1 vote

What is Unit in Scala? What is the difference between Java’s void and Scala’s Unit?

+4 votes

Like Java’s Package level Access Modifier, is there anything same concept in Scala? How to provide Package level Access Modifier in Scala?

0 votes

Explain the main difference between List and Stream in Scala Collection API? How do we prove that difference? When do we choose Stream?

...