Skip to content

Commit 76b013a

Browse files
committed
Checkpoint
1 parent f6a1b4b commit 76b013a

38 files changed

+1446
-30
lines changed

src/SUMMARY.md

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,15 @@ Make them do one. -->
606606
# Data Types VI
607607

608608
- [Collections](./collections.md)
609-
- [Collection]()
610-
- [List]()
611-
- [Map]()
612-
- [Set]()
609+
- [List](./collections/list.md)
610+
- [Map](./collections/map.md)
611+
- [Set](./collections/set.md)
613612
- [Arrays](./collections/arrays.md) <!-- Odd duck out, Arrays.asList -->
613+
- [UnsupportedOperationException](./collections/unsupported_operation_exception.md)
614+
- [Factories](./collections/factories.md)
615+
- [Specificity](./collections/specificity.md)
616+
617+
614618
- [Multi-Dimensional Arrays](./multi_dimensional_arrays.md)
615619
- [Declaration](./multi_dimensional_arrays/declaration.md)
616620
- [Array Initializers](./multi_dimensional_arrays/array_initializers.md)
@@ -664,21 +668,22 @@ Make them do one. -->
664668
# Code Structure VII
665669
- [Interfaces II](./interfaces_ii.md)
666670
- [Default Methods](./interfaces_ii/default_methods.md)
667-
- [Interface Extension]()
668-
- [Static Methods]()
669-
- [Private Static Methods]()
671+
- [Interface Extension](./interfaces_ii/interface_extension.md)
672+
- [Static Methods](./interfaces_ii/static_methods.md)
673+
- [Static Fields](./interfaces_ii/static_fields.md)
670674
- [Class Extension](./class_extension.md)
671-
- [Extend a Class]()
672-
- [Protected Fields]()
673-
- [Protected Methods]()
674-
- [Relation to Interfaces]()
675-
- [Relation to Encapsulation]()
675+
- [Extend a Class](./class_extension/extend_a_class.md)
676+
- [Inheritance](./class_extension/inheritance.md)
677+
- [Override](./class_extension/override.md)
678+
- [Protected](./class_extension/protected.md)
679+
- [Abstract Classes](./class_extension/abstract_classes.md)
680+
- [Abstract Methods](./class_extension/abstract_methods.md)
681+
- [Relation to Interfaces](./class_extension/relation_to_interfaces.md)
682+
- [Relation to Encapsulation](./class_extension/relation_to_encapsulation.md)
676683
- [Final Classes](./class_extension/final_classes.md)
677-
- [Abstract Classes](./abstract_classes.md)
678-
- [Relation to Interfaces]()
679684

680685

681-
# Data Types IV
686+
# Data Types VII
682687

683688
- [Niche Numerics](./niche_numerics.md)
684689
- [byte](./niche_numerics/byte.md)
@@ -698,6 +703,22 @@ Make them do one. -->
698703
- [The Unnamed Module](./modules/the_unnamed_module.md)
699704
- [Module Imports](./modules/module_imports.md)
700705
- [Multi-Module Directory Layout]()
706+
- [Lambdas](./lambdas.md)
707+
- [Functional Interfaces](./lambdas/functional_interfaces.md)
708+
- [@FunctionalInterface](./lambdas/functional_interface_annotation.md)
709+
- [Streams]()
710+
711+
# Sharing Code
712+
713+
- [Compilation](./compilation.md)
714+
- [javac]()
715+
- [Packaging]()
716+
- [jar]()
717+
- [Documentation]()
718+
- [javadoc]()
719+
- [Documentation Comments](./documentation/documentation_comments.md)
720+
- [Distribution]()
721+
- [jlink]()
701722

702723
<!---
703724
PROJECT IDEAS:
@@ -764,10 +785,6 @@ PROJECT IDEAS:
764785
- [Primitive Patterns]()
765786
- [Record Patterns]()
766787
767-
# Code Structure VII
768-
769-
- [Modules]()
770-
771788
-->
772789
<!--
773790
@@ -836,11 +853,9 @@ Generics
836853
Maven
837854
XML
838855
Pom
839-
byte, short, long
840856
Multiversal Equality
841857
Reflection
842858
Cover Invoking *public* constructs
843-
Annotations
844859
Javadoc and Documentation comments
845860
-->
846861

src/abstract_classes.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/class_extension.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# Class Extension
22

3+
Just like interfaces can extend other interfaces, classes can extend other classes.
4+
5+
```java
6+
class BodyOfWater {}
7+
8+
class Ocean extends BodyOfWater {}
9+
```
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Abstract Classes
2+
3+
Abstract classes are classes which you cannot make an instance of directly.[^concrete]
4+
5+
```java,does_not_compile
6+
abstract class Plant {}
7+
8+
void main() {
9+
// You cannot make an instance because
10+
// it is an abstract class.
11+
var p = new Plant();
12+
}
13+
```
14+
15+
The use-case for these is making classes which are designed to be subclassed.
16+
An example of this that comes with Java is `AbstractList`.
17+
18+
`AbstractList` defines most of the methods required by the `List` interface and is intended to
19+
lower the effort required to make a custom `List` implementation.
20+
21+
```java
22+
import java.util.AbstractList;
23+
24+
// This subclass is a List containing the numbers 5 and 7
25+
class FiveAndSeven
26+
// Almost all of the implementation is inherited from AbstractList
27+
extends AbstractList<Integer> {
28+
29+
@Override
30+
public Integer get(int index) {
31+
return switch (index) {
32+
case 0 -> 5;
33+
case 1 -> 7;
34+
default -> throw new IndexOutOfBoundsException();
35+
};
36+
}
37+
38+
@Override
39+
public int size() {
40+
return 2;
41+
}
42+
}
43+
44+
void main() {
45+
var l = new FiveAndSeven();
46+
IO.println(l);
47+
}
48+
```
49+
50+
[^concrete]: "Abstract" as a term here means something close to "not in reality." You will hear people refer to non-abstract classes as "concrete" classes.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Abstract Methods
2+
3+
Methods on an `abstract` class may themselves be marked `abstract`.
4+
5+
```java
6+
abstract class BodyOfWater {
7+
long depth;
8+
9+
BodyOfWater(long depth) {
10+
if (this.depth < 0) {
11+
throw new IllegalArgumentException();
12+
}
13+
this.depth = depth;
14+
}
15+
16+
// All classes which extend BodyOfWater must
17+
// define what happens when you swim.
18+
abstract void swim();
19+
}
20+
```
21+
22+
Any non-abstract class extending from an `abstract` class must give a definition
23+
for `abstract` methods.
24+
25+
```java
26+
abstract class BodyOfWater {
27+
long depth;
28+
29+
BodyOfWater(long depth) {
30+
if (this.depth < 0) {
31+
throw new IllegalArgumentException();
32+
}
33+
this.depth = depth;
34+
}
35+
36+
abstract void swim();
37+
}
38+
39+
class Lake extends BodyOfWater {
40+
// If you didn't define this it wouldn't work
41+
@Override
42+
void swim() {
43+
IO.println("Relaxing time");
44+
}
45+
}
46+
```
47+
48+
But if an abstract class is being extended by another abstract class, you don't need to.
49+
50+
```java,no_run
51+
abstract class Liquid {
52+
abstract double viscosity();
53+
}
54+
55+
// Water doesn't need to define volume()
56+
// because Water is abstract
57+
abstract class Water extends Liquid {
58+
abstract double purity();
59+
}
60+
61+
// But as soon as you get to a "concrete" implementation
62+
// you need to provide definitions
63+
class TownWater extends Water {
64+
@Override
65+
double viscosity() {
66+
return 0.01;
67+
}
68+
69+
@Override
70+
double purity() {
71+
return 0.99;
72+
}
73+
}
74+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Extend a Class
2+
3+
For a class to extend another one you need to write `extends` and then the name of the class
4+
which is being extended.
5+
6+
```java
7+
class BodyOfWater {}
8+
9+
class Ocean extends BodyOfWater {}
10+
```
11+
12+
If the class being extended has non-zero argument constructors,
13+
the subclass must pass arguments to those constructors in it's constructor
14+
using `super()`.
15+
16+
```java
17+
class BodyOfWater {
18+
long depth;
19+
20+
BodyOfWater(long depth) {
21+
if (this.depth < 0) {
22+
throw new IllegalArgumentException();
23+
}
24+
this.depth = depth;
25+
}
26+
}
27+
28+
class Ocean extends BodyOfWater {
29+
String name;
30+
31+
Ocean(String name, long depth) {
32+
this.name = name;
33+
// Before you exit, you must pass
34+
// arguments to the super-class constructor.
35+
super(depth);
36+
}
37+
}
38+
39+
void main() {
40+
Ocean pacific = new Ocean("Pacific", 36201L);
41+
IO.println(
42+
"The " + pacific.name +
43+
" ocean is " + pacific.depth +
44+
"' deep."
45+
);
46+
}
47+
```

src/class_extension/final_classes

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,42 @@
11
# Final Classes
2+
3+
If a class is marked `final` it cannot be extended.
4+
5+
```java,does_not_compile
6+
final class Apple {}
7+
8+
// Cannot extend a final class
9+
class RedApple extends Apple {}
10+
~void main() {}
11+
```
12+
13+
Because thinking about what the implications are if someone extends a class is hard,
14+
it is reasonable to always declare your classes as `final` unless you have some reason not to.
15+
16+
```java
17+
// It's not that something bad would happen if someone
18+
// extended this class. It's that not having to think
19+
// about it is useful.
20+
final class Saltine {
21+
int saltiness;
22+
23+
Saltine(int saltiness) {
24+
this.saltiness = saltiness;
25+
}
26+
}
27+
```
28+
29+
Records and Enums are always implicitly final.
30+
31+
```java,does_not_compile
32+
record Pos(int x, int y) {}
33+
34+
// Records are final
35+
class Pos2 extends Pos {}
36+
37+
enum StopLight { RED, YELLOW, GREEN }
38+
39+
// Enums are final
40+
class StopLight2 extends StopLight {}
41+
~class Main {void main() {}}
42+
```

src/class_extension/inheritance.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Inheritance
2+
3+
When one class extends another we say that it is "inheriting"
4+
from the superclass.
5+
6+
What this means is that all the fields and methods of the class being extended
7+
carry over into the extending class.
8+
9+
```java
10+
class BodyOfWater {
11+
long depth;
12+
13+
BodyOfWater(long depth) {
14+
if (this.depth < 0) {
15+
throw new IllegalArgumentException();
16+
}
17+
this.depth = depth;
18+
}
19+
20+
void swim() {
21+
if (depth > 50) {
22+
IO.println("You have drowned");
23+
}
24+
else {
25+
IO.println("You are fine");
26+
}
27+
}
28+
}
29+
30+
class Ocean extends BodyOfWater {
31+
String name;
32+
33+
Ocean(String name, long depth) {
34+
this.name = name;
35+
super(depth);
36+
}
37+
}
38+
39+
void main() {
40+
BodyOfWater pacific = new Ocean("Pacific", 36201L);
41+
pacific.swim(); // The swim method is "inherited"
42+
}
43+
```

0 commit comments

Comments
 (0)