class Car(var maker : String, var year : Int)
{
// second constructor
def this(m: String)
{
this(m, 0);
}
def getMaker() : String = {return maker;}
override def toString : String = {return "Maker: %10s; Year: %10d".format(maker, year)}
}
object Demo
{
def main(args: Array[String])
{
val c = new Car("Buick", 1998); // immutable reference
println(c); // Buick, 1998
val c2 = new Car("Honda");
println(c2); // Honda, 0
}
}
class Vehicle(val m : String) // immutable parameters
{
var maker : String = m; // mutable fields
def disp() {println("Maker: "+maker)}
// override toString
override def toString : String = {return "Maker: %10s;".format(maker)}
}
class Car(override val m : String, var year : Int) extends Vehicle(m) // override parameter in the parent
{
// override the function defined in the parent
override def disp()
{
super.disp(); // call the function defined in the parent
println("Year: "+year)
}
// override toString
override def toString : String = {return "Maker: %10s; Year: %10d".format(maker, year)}
}
object Demo
{
def main(args: Array[String])
{
val c = new Car("Buick", 1998); // immutable reference
println(c); // Buick, 1998
c.disp();
}
}