-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
complete example for sect17 (packages)
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package section17; | ||
|
||
import section17.vessel.RectangularContainer; | ||
|
||
/** | ||
* This class demonstrates the use of packages and the access right of accessing fields of classes in other package. | ||
*/ | ||
class UseVessels { | ||
public static void main(String[] argv) { | ||
Cube testCube = new Cube(42); | ||
System.out.println(testCube); | ||
|
||
RectangularContainer testRect = testCube; // casted to superclass | ||
/** | ||
* In the following line, Cube.toString() is still invoked because testRect is "actually" a Cube. | ||
* The behavior is different from how *fields* are accessed (e.g. testRect.foobar). | ||
*/ | ||
System.out.println(testRect.toString()); | ||
} | ||
} | ||
|
||
// Private class based on section9.Cube | ||
class Cube extends RectangularContainer { | ||
|
||
public Cube(double side) { | ||
super(side, side, side); | ||
} | ||
|
||
public String toString() { | ||
return String.format("Cube(side=%f)", width); // width is accessible since it is `protected` in Rect'Container | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package section17.vessel; | ||
|
||
/** | ||
* Based on section9.BarrelContainer, but with tighter member controls. | ||
*/ | ||
public class Barrel extends Vessel { | ||
|
||
private final double radius, height; // 'private final' such that it's not directly accessible from outside | ||
|
||
public Barrel(double radius, double height) { | ||
this.radius = radius; | ||
this.height = height; | ||
} | ||
|
||
public double capacity() { | ||
return Math.PI * radius * radius * height; | ||
} | ||
|
||
public String toString() { | ||
return String.format("section17.vessel.Barrel(radius=%f, height=%f)", radius, height); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package section17.vessel; | ||
|
||
/** | ||
* Based on section9.RectangularContainer, but with different member controls. | ||
*/ | ||
public class RectangularContainer extends Vessel { | ||
|
||
protected final double length, width, height; // Want this to be accessible in subclasses wherever they are | ||
|
||
public RectangularContainer(double length, double width, double height) { | ||
this.length = length; | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
public double capacity() { | ||
return length * width * height; | ||
} | ||
|
||
public String toString() { | ||
return String.format("section17.vessel.RectangularContainer(length=%f, width=%f, width=%f)", | ||
length, width, height); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package section17.vessel; | ||
|
||
/** | ||
* Mostly the same hierarchy as section9.LiquidContainer, but with intentional package separation. | ||
*/ | ||
public abstract class Vessel { | ||
|
||
private double contents; | ||
public abstract double capacity(); | ||
|
||
public final void fill(double amount) { | ||
contents = Math.min(capacity(), contents + amount); | ||
} | ||
|
||
// Only defines a getter for contents | ||
public final double getContents() { | ||
return contents; | ||
} | ||
|
||
} |