Chapter 7, Exercises 8 and 10
Been real busy at work, and in Grass Valley, Tahoe, Las Vegas,
and home in Ben Lomond.
Here are exercises 8 and 10 from chapter 7.
8. Create a class as abstract without including any abstract methods and verify that you cannot create any instances of that class.
package x;
abstract class c7x8_abs {
var data : Int = 12;
def member = W.rn( "member " + 12 );
}
object c7x6 {
def main ( args: Array[String] ) = {
var y = new c7x8_abs;
}
}
% scalac c7x8.scala
c7x8.scala:12: class c7x8_abs is abstract, so it cannot be instantiated
var y = new c7x8_abs;
^
one error found
Pretty clear error message.
Next:
10. Modify Exercise 6 so that it demonstrates the order of
initialization of the base classes and derived classes. Now add
member objects to both the base and derived classes and show the
order in which their initialization occurs during construction.
Here's the first half of that exercise, c7x10a.scala:
package x;
class Rodent( s: String ) {
W.rn( "Rodent(" + s + ") ctor top" );
var rname : String = s;
def chew = W.rn( rname + " chews." );
def poop = W.rn( rname + " poops." );
W.rn( "Rodent(" + s + ") ctor end" );
}
class Mouse extends Rodent("Mouse") {
W.rn( "Mouse ctor top" );
override def chew = W.rn( rname + " chews cheese." );
override def poop = W.rn( rname + " poops pellets." );
W.rn( "Mouse ctor end" );
}
class Gerbil extends Rodent("Gerbil") {
W.rn( "Gerbil ctor top" );
override def chew = W.rn( rname + " chews rgere." );
W.rn( "Gerbil ctor end" );
}
class Hamster extends Rodent("Hamster") {
W.rn( "Hamster ctor top" );
override def poop = W.rn( rname + " poops in its cage." );
W.rn( "Hamster ctor end" );
}
class Capybara extends Rodent("Capybara") {
W.rn( "Capybara ctor top" );
override def chew = W.rn( rname + " chews gauchos." );
W.rn( "Capybara ctor end" );
}
// A "factory" that randomly creates rodents:
class RandomRodentGenerator {
private var rand = new java.util.Random();
def next : Rodent = {
rand.nextInt(4) match {
case 0 => new Mouse;
case 1 => new Gerbil;
case 2 => new Hamster;
case 3 => new Capybara;
}
}
}
object c7x10a {
private var gen = new RandomRodentGenerator;
def main ( args: Array[String] ) = {
var s = new Array[Rodent]( 9 );
for( val i <- Iterator.range(0, s.length) )
s(i) = gen.next;
for( val i <- Iterator.range(0, s.length) ) {
s(i).chew; s(i).poop;
}
}
}
And here's the output from that first half of the exercise,
showing that the base class constructor is called before the
derived class constructor.
Rodent(Capybara) ctor top
Rodent(Capybara) ctor end
Capybara ctor top
Capybara ctor end
Rodent(Capybara) ctor top
Rodent(Capybara) ctor end
Capybara ctor top
Capybara ctor end
Rodent(Hamster) ctor top
Rodent(Hamster) ctor end
Hamster ctor top
Hamster ctor end
Rodent(Hamster) ctor top
Rodent(Hamster) ctor end
Hamster ctor top
Hamster ctor end
Rodent(Capybara) ctor top
Rodent(Capybara) ctor end
Capybara ctor top
Capybara ctor end
Rodent(Mouse) ctor top
Rodent(Mouse) ctor end
Mouse ctor top
Mouse ctor end
Rodent(Hamster) ctor top
Rodent(Hamster) ctor end
Hamster ctor top
Hamster ctor end
Rodent(Hamster) ctor top
Rodent(Hamster) ctor end
Hamster ctor top
Hamster ctor end
Rodent(Capybara) ctor top
Rodent(Capybara) ctor end
Capybara ctor top
Capybara ctor end
Capybara chews gauchos.
Capybara poops.
Capybara chews gauchos.
Capybara poops.
Hamster chews.
Hamster poops in its cage.
Hamster chews.
Hamster poops in its cage.
Capybara chews gauchos.
Capybara poops.
Mouse chews cheese.
Mouse poops pellets.
Hamster chews.
Hamster poops in its cage.
Hamster chews.
Hamster poops in its cage.
Capybara chews gauchos.
Capybara poops.
For the second half, we add member objects to both the base
and derived classes ...
%%% diff c7x10[ab].scala
7a8,10
> class mob(c:String, s:String) {
> W.rn( "member object in class " + c + " with s " + s );
> }
13a17
> var Rmob = new mob("Rodent", s);
19a24
> var Mmob = new mob("Mouse", "mickey");
24a30
> var Gmob = new mob("Gerbil", "gates");
29a36
> var Hmob = new mob("Hamster", "like friendster, but for pigs");
34a42
> var Cmob = new mob("Capybara", "brazilian");
48c56
< object c7x10a {
---
> object c7x10b {
%%%
And the output (not provided) shows that each of those member
objects is initialized just after the evaluation of the default
contsructor for the class that contains it.
10:13:54 PM