Chapter 6, Exercise 10
Skipping forward a ways -- the next few exercises modify some classes that Bruce provided in the chapter, and some depend on Java's ability to have classes that lack default constructors, which I think is something you can't do in Scala. Anyway:
10. Create a class called Root that contains an instance of
each of the classes (that you also create) named Component1,
Component2, and Component3. Derive a class Stem from Root that
also contains an instance of each component. All classes should
have default constructors that print a message about that class.
Well, OK, but I think I'll call them P, Q, and R instead.
package x;
class P { W.rn( "P" ); }
class Q { W.rn( "Q" ); }
class R { W.rn( "R" ); }
class Root {
val rpm = new P;
var rqm = new Q;
var rrm = new R;
W.rn( "Root" );
}
class Stem extends Root {
val spm = new P;
var sqm = new Q;
var srm = new R;
W.rn( "Stem" );
}
object c6x10 {
def main ( args : Array[String] ) = {
var main_R = new Root;
val main_S = new Stem;
}
}
Here's the output that I get:
P
Q
R
Root
P
Q
R
Root
P
Q
R
Stem
I'll skip 11 and 12, and investigate overloading next.
11:58:13 PM