Constructors
|
|
Combining statements desired: |
|
---|---|
|
|
Similar to Figure 207, “Method definition syntax ” but:
[access modifier] constructorName (listOfArguments) {
[constructor body]
}
- Empty argument list
-
Default constructor e.g.
obj = new MyClass()
. - Non-empty argument list
-
Non-default constructor e.g. :
obj = new String("xyz");
-
Can only be executed on object creation.
-
Are being called prior to any non-constructor method.
-
Only one of potentially multiple constructors will be executed exactly one time.
However nesting is possible.
|
|
|
public class Rectangle { int width, height; ┏━▶ public Rectangle(int width, ┃ int height){ ┃ this.width = width; ┃ this.height = height; ┃ } ┃ public Rectangle() { ┣━◀━━ this(1, 1); ❶ ┃ } ┃ public Rectangle( ┃ int widthAndHeight) { ┗━◀━━ this(widthAndHeight, ❷ widthAndHeight); } } |
|
Equivalent: Rectangle r =
new Rectangle(); |
|
---|---|
|
|
|
|
|
No. 91
Modeling geometry objects: Rectangles
Q: |
We want to represent rectangles being defined by width and height to allow for the subsequently demonstrated operations:
You may start from the following
|
||||||||||
A: |
First we define two instance variables
Next we allow for changing these two parameters:
Note the subtle implementation difference between
Both ways are perfectly legal. The complete implementation including all remaining methods reads:
|
No. 92
Modeling circles
Q: |
This exercise is very similar to Modeling geometry objects: Rectangles . With respect to the upcoming section on inheritance its presence will be justified later by the section called “Geometry classes reconsidered”. We provide a corresponding class
Instances of this class shall be usable in the following fashion:
Hint: Obviously you'll have to define an instance variable within Circle to keep track of its current radius value. All methods mentioned above simply depend on this single value. |
A: |
We define an instance variable
Next we implement our method to change a circle's radius:
Note that we have chosen a different value for the method's formal radius parameter to be “r” rather than “radius”. Many people prefer to use radius here making it easier for a programmer to recognize the expected name in the generated Javadoc™:
This requires the usage of the The rest of the implementation is (quite) straightforward. A complete class reads:
|
No. 93
Adding translations and SVG export.
Q: |
We want to add more features to our
The following code snippet may serve to illustrate the
intended use of
Implement the method
You may copy this output into a file
|
A: |
|
No. 94
Extending the employee example.
Q: |
Extend the Employee example from chapter 4 of [Kurniawan] by adding two methods:
Run your implementation by a separate class
The expected output reads: Age:25 Salary:30000.00 Raising salary by 2% Age:25 Salary:30600.00 |
A: |
|
No. 95
Refining access to an employee's attributes
Q: |
The previous exercise featured two classes:
Currently both classes (hopefully!) reside in the same
package company. We will assume the two
attributes age and
salary yet have no
Does the following code work?
Modify your application by:
What do you observe? Explain this result. How do you solve the new problem? TipRead the section on |
|||||||||
A: |
Moving the
The movement also adds an additional
Unfortunately a
This error message is in accordance with Table 4.1 from
the “Encapsulation and Access Control” section of
[Kurniawan]: The attributes
age and salary have no
access modifiers at all. Hence “default” access
prohibits classes belonging to other packages accessing the
attributes in question. More clearly: Members from class
There are two possible ways to overcome this problem:
The novice reader may question the advantage of the second approach: Alien classes have essentially the same means changing an employee's salary as with the first approach. There is however one apparent benefit: Developers do have better control including bug tracking options on offer:
|
No. 96
File system representation
Q: |
In the Employee
exercise you moved the How are the corresponding Java™
source files TipUse your OS file system explorer. |
A: |
Depending on your type of project (Java, Maven,...) both
your
Packages give rise to further sub directories both for source code and compiler generated byte code. The following example is a minor variation of our employee example. The model package has been refactored into company.model: Figure 240.
Employee example package
hierarchy
|
No. 97
Your personal String
class
Q: |
This exercise is about using packages for resolving class name clashes. Usually you won't actually define a class String of your own next to java.lang.String. Define your own
The expected output reads: Name: John Hello 'John' Tip
|
||||||||||
A: |
We implement our private
There are basically two ways using
de.hdm_stuttgart.mi.sd1.tooling.
|