Skip to content

Commit

Permalink
complete example for sect17 (packages)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymzong committed Apr 2, 2018
1 parent 6eaf48f commit 62e612a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/section17/UseVessels.java
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
}
}
22 changes: 22 additions & 0 deletions src/section17/vessel/Barrel.java
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);
}
}
25 changes: 25 additions & 0 deletions src/section17/vessel/RectangularContainer.java
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);
}

}
20 changes: 20 additions & 0 deletions src/section17/vessel/Vessel.java
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;
}

}

0 comments on commit 62e612a

Please sign in to comment.