28.08.2016 Views

scala_tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Scala<br />

class Super {<br />

}<br />

protected def f() { println("f") }<br />

class Sub extends Super {<br />

}<br />

f() //Ok : Sub class is the child class of Super class<br />

class Other {<br />

}<br />

(new Super).f() // Error: f is not accessible<br />

}<br />

The access to f in class Sub is OK because f is declared protected in ‘Super’ class and ‘Sub’<br />

class is a subclass of Super. By contrast the access to f in ‘Other’ class is not permitted,<br />

because class ‘Other’ does not inherit from class ‘Super’. In Java, the latter access would be<br />

still permitted because ‘Other’ class is in the same package as ‘Sub’ class.<br />

Public Members<br />

Like private and protected members, it is not required to specify Public keyword for Public<br />

members. There is no explicit modifier for public members. Such members can be accessed<br />

from anywhere.<br />

Following is the example code snippet to explain protected member:<br />

class Outer {<br />

class Inner {<br />

def f() { println("f") }<br />

class InnerMost {<br />

f() // OK<br />

}<br />

}<br />

(new Inner).f() // OK because now f() is public<br />

31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!