A private member is visible only inside the class or object that contains the member definition
class Car(val m : String) // immutable paramters
{
private var maker : String = m;
override def toString : String = {return "Maker: %10s".format(maker)}
}
object Demo
{
def main(args: Array[String])
{
val c = new Car("Buick"); // immutable reference
println(c); // Buick
//c.maker = "Honda"; // error
}
}
A protected member is only accessible from subclasses of the class in which the member is defined
class Vehicle(val m : String)
{
protected var maker : String = m;
def disp() {println("Maker: "+maker)}
}
class Car(override val m : String, var year : Int) extends Vehicle(m)
{
override def disp()
{
println("Maker: "+maker+" Year: "+year)
}
}
object Demo
{
def main(args: Array[String])
{
val c = new Car("Buick", 1998); // immutable reference
c.disp();
}
}
default, not required to specify Public keyword for Public members, such members can be accessed from anywhere
class Car(val m : String) // immutable paramters
{
var maker : String = m;
override def toString : String = {return "Maker: %10s".format(maker)}
}
object Demo
{
def main(args: Array[String])
{
val c = new Car("Buick"); // immutable reference
println(c); // Buick
c.maker = "Honda";
println(c); // Honda
}
}