Access and visibility

In this subsection, we will try to understand the access and visibility of Scala variables and different data types in the OOP paradigm. Let's have a look at access modifiers in Scala. A similar one for Scala:

Modifier Class Companion Object Package Subclass Project
Default/No modifier Yes Yes Yes Yes Yes
Protected Yes Yes Yes No No
Private Yes Yes No No No

 

Public members: Unlike a private and protected member, it is not required to specify the public keyword for public members. There is no explicit modifier for public members. Such members can be accessed from anywhere. For example:

class OuterClass { //Outer class
class InnerClass {
def printName() { println("My name is Asif Karim!") }

class InnerMost { //Inner class
printName() // OK
}
}
(new InnerClass).printName() // OK because now printName() is public
}

Private members: A private member is visible only inside the class or object that contains the member definition. Let's see an example, as follows:

package MyPackage {
class SuperClass {
private def printName() { println("Hello world, my name is Asif Karim!") }
}
class SubClass extends SuperClass {
printName() //ERROR
}
class SubsubClass {
(new SuperClass).printName() // Error: printName is not accessible
}
}

Protected members: A protected member is only accessible from subclasses of the class in which the member is defined. Let's see an example, as follows:

package MyPackage {
class SuperClass {
protected def printName() { println("Hello world, my name is Asif
Karim!") }
}
class SubClass extends SuperClass {
printName() //OK
}
class SubsubClass {
(new SuperClass).printName() // ERROR: printName is not accessible
}
}

Access modifiers in Scala can be augmented with qualifiers. A modifier of the form private[X] or protected[X] means that access is private or protected up to X, where X designates an enclosing package, class, or singleton object. Let's see an example:

package Country {
package Professional {
class Executive {
private[Professional] var jobTitle = "Big Data Engineer"
private[Country] var friend = "Saroar Zahan"
protected[this] var secret = "Age"

def getInfo(another : Executive) {
println(another.jobTitle)
println(another.friend)
println(another.secret) //ERROR
println(this.secret) // OK
}
}
}
}

Here's a short note on the preceding code segment:

  • Variable jboTitle will be accessible to any class within the enclosing package Professional
  • Variable friend will be accessible to any class within the enclosing package Country
  • Variable secret will be accessible only to the implicit object within instance methods (this) only

If you look at the preceding examples, we used the keyword package. However, we have not discussed this so far. But don't worry there will be a dedicated section later in this chapter. The constructor is a strong feature for any objected-oriented programming language. Scala is not an exception. Now, let's have a short overview of the constructor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.2