Chapter 5, Exercise 5
Chapter 5, Exercise 5 asks us to
"Create a class with public, private, protected, and
package-access fields and method members. Create an object
of this class and see what kind of compiler messages you get
when you try to access all the class members. Be aware that
classes in the same directory are part of the "default" package."
I don't think Scala has "public"; members default to being
public. And I don't see a "package" access modifier either.
Here's THECLAPP's version of c5x5 and c5x6.
package x;
class c5x5c {
private var vpvt = 12;
protected var vprot = 13;
var vpub = 14;
private def dpvt (i:Int) :Int = 2*i;
protected def dprot (j:Int):Int = 3*j;
def dpub (k:Int):Int = 4*k;
// accessing
def internal_axes (m:Int):Int = {
vpvt + vprot + vpub +
dpvt(vpvt) + dprot(vprot) + dpub(vpub) - m
}
}
object c5x5 {
def main (args: Array[String] ) = {
var ob = new c5x5c;
var z = ob.internal_axes( 158 );
Console.println(z);
Console.println( "vpub " + ob.vpub);
Console.println( "dpub(9) " + ob.dpub(9) );
// The accesses below here get error messages
Console.println( "vpvt " + ob.vpvt);
Console.println( "dpvt(8) " + ob.dpvt(8) );
Console.println( "vprot " + ob.vprot);
Console.println( "dprot(7) " + ob.dprot(7) );
}
}
Here's the error messages scalac gives me when I do include the red lines. (The line numbers have been changed because Radio screws up the blockquote if it has a blank line in it.)
c5x5_not.scala:34: variable vpvt cannot be accessed in x.c5x5c
Console.println( "vpvt " + ob.vpvt);
^
c5x5_not.scala:35: method dpvt cannot be accessed in x.c5x5c
Console.println( "dpvt(8) " + ob.dpvt(8) );
^
c5x5_not.scala:36: variable vprot cannot be accessed in x.c5x5c
Console.println( "vprot " + ob.vprot);
^
c5x5_not.scala:37: method dprot cannot be accessed in x.c5x5c
Console.println( "dprot(7) " + ob.dprot(7) );
^
four errors found
11:37:44 PM