02115 Java Programming | ||
Autumn 2011 |
D1 d2 = new D2();
The variable d2
will refer to an instance of class
D2
thus defining its dynamic type. The details are as
follows (refer to the last seven lines of page 28 in the PS
textbook):
f
of the D2
-instance is
initialized to the default value 0
D1
implies
an application of method m2()
m2()
prints out D1.m2
and then applies method m1()
m1()
of class D1
has
been overriden in the subclass D2
the method
m1()
of class D2
will be executed
because the dynamic class type of d2
is D2
m1()
from class D2
prints out D2.m1:0
. The value 0
is
the default value given to f
previously
f = 7
from the constructor
D2()
gives f
its final value
The lectures of the last week has covered most of the Chapters 8 and 9. And if you have followed up by working through the text as told in the section After the lectures on the last Week plan you have become familiar with the concepts and are able to use them.
On page 291 a reference was given to Joshua Bloch's Effective Java, a textbook which I strongly recommend. I was not able to look up sample chapters of the book on the net, but I have the printed book.
Before buying the book mentioned above just take a look at the paper Java performance written by Peter Sestoft and referenced at the course home page link to Some Java supplement. The paper gives some advices on improving the execution of Java programs.
Do you think a class can inherit from more than one superclass ?
In the textbook PS section 9.5 (page 22) the form of an
extends-clause is described by
class C extends B { ... }
and we are told, that class C
is a subclass and hence a
subtype (section 5.5) of B
. And from that we can judge, that
it does not make sense if a class should be a subtype of two
or more probably independant super classes.
Thus: A class can only have one super class.
The BlueJ environment has surprised me when I put a new BlueJ project
on the subpage 'Some BlueJ projects': I just defined a project
consisting of the two files from Example 27 and
Example 28 of the PS textbook (page 23) without
spliting up Example 27 into four separate files.
Try to save the .jar
, and then compile and run the
main()
method from within BlueJ.
The Chapters 10 and 12 present important concepts and very
useful Java constructs to be used by the experienced programmer.
Chapter 10 introduce abstract classes and interfaces
and thus gives the full background for understanding the description
in the PS textbook on collections, refer to the overview on
page 92. Chapter 12 introduce the SortedMap
interface and the concrete classes TreeSet
and TreeMap
.
As an introduction to this week, prepare yourself by study the BK textbook to the following extent:
But many subjects of the chapters are left for personal studies.
At the end of this week you are supposed to be familiar with the following parts of the BK textbook:
In the Sections 10.1 and 10.2 (pages 300-316) focus on how the solution is structured and build up by use of many classes and study how the jobs are delegated out.
Sections 10.3 and 10.4 (pages 316-324) introduce abstract classes and abstract methods. As an easy-to-read introduction just start with Example 27, page 23 of the PS textbook:
capacity()
of
class Vessel
has to be abstract
because the value it should return depends on the
specific shape of each kind of vessel.
capacity()
one takes advantage of
the fact that a cube is a special proportioned tank.
Sections 10.5 and 10.6 (pages 324 - 334) are dedicated
to multiple inheritance achieved by use of interfaces.
The subsections 10.6.2 and 10.6.3 (pages 330-331) illustrates
the precise description of section 13.2 in the PS
textbook (page 60). Also read the Examples 85 and
86 on page 61.
Subsection 10.6.4 settles the most important characteristic
of interfaces to be that they completely separate the
definition of the functionality from its implementation.
try
...catch
mechanism. The
statements whose execution may cause an exception to
be thrown should be placed in the try
branch
and the handling of the exception should be placed in
corresponding catch
branches with one branch
for each kind of exception.
Exception
class.
String
should be used as argument when a
main()
method is applied from within the
BlueJ environment
private
, protected
and public
TreeMap
constructors
on page 100
Map<Class, Color> colors
in the class
SimulatorView
(open the editor on that class
using the foxes-and.rabbits-v1 project) to hold
information of the colors
used in Figure 10.2 (page 303) of the BK
textbook. Primaryly, refer to the first sentence of
section 24.1 of the PS textbook.view.setColor()
on page 312 of BK. As keys are used Rabbit.class
and Fox.class
, respectively, and the corresponding
values are the colors Color.orange
and
Color.blue
, respectively
Newest edition: 26. September (minor corrections to the
language and the addition of a reference to the last seven lines
of page 28 in the PS textbook)
Previous editions:
- 21. September (comments on PS -
Example60 added and pages 407-423 of the BK textbook has
been skiped to next week)
- 20. September
- 8. April (just the heading and footing)