Skip to content

Commit 2fc0790

Browse files
committed
Add Java inheritance and polymorphism module
1 parent b18987f commit 2fc0790

13 files changed

Lines changed: 270 additions & 13 deletions

File tree

CONCEPT_INDEX.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Use this page to browse the same concept across every active language track.
3939
| structs-and-classes | [C++](languages/cpp/03-advanced/structs-and-classes/README.md) | [C#](languages/csharp/03-advanced/structs-and-classes/README.md) | [Go](languages/go/03-advanced/structs-and-classes/README.md) | [Java](languages/java/03-advanced/structs-and-classes/README.md) | [Python](languages/python/03-advanced/structs-and-classes/README.md) | [TypeScript](languages/typescript/03-advanced/structs-and-classes/README.md) |
4040
| constructors-and-invariants | [C++](languages/cpp/03-advanced/constructors-and-invariants/README.md) | [C#](languages/csharp/03-advanced/constructors-and-invariants/README.md) | [Go](languages/go/03-advanced/constructors-and-invariants/README.md) | [Java](languages/java/03-advanced/constructors-and-invariants/README.md) | [Python](languages/python/03-advanced/constructors-and-invariants/README.md) | [TypeScript](languages/typescript/03-advanced/constructors-and-invariants/README.md) |
4141
| copy-and-move-semantics | [C++](languages/cpp/03-advanced/copy-and-move-semantics/README.md) | [C#](languages/csharp/03-advanced/copy-and-move-semantics/README.md) | [Go](languages/go/03-advanced/copy-and-move-semantics/README.md) | Planned | [Python](languages/python/03-advanced/copy-and-move-semantics/README.md) | [TypeScript](languages/typescript/03-advanced/copy-and-move-semantics/README.md) |
42-
| inheritance-and-polymorphism | [C++](languages/cpp/03-advanced/inheritance-and-polymorphism/README.md) | [C#](languages/csharp/03-advanced/inheritance-and-polymorphism/README.md) | [Go](languages/go/03-advanced/inheritance-and-polymorphism/README.md) | Planned | [Python](languages/python/03-advanced/inheritance-and-polymorphism/README.md) | [TypeScript](languages/typescript/03-advanced/inheritance-and-polymorphism/README.md) |
42+
| inheritance-and-polymorphism | [C++](languages/cpp/03-advanced/inheritance-and-polymorphism/README.md) | [C#](languages/csharp/03-advanced/inheritance-and-polymorphism/README.md) | [Go](languages/go/03-advanced/inheritance-and-polymorphism/README.md) | [Java](languages/java/03-advanced/inheritance-and-polymorphism/README.md) | [Python](languages/python/03-advanced/inheritance-and-polymorphism/README.md) | [TypeScript](languages/typescript/03-advanced/inheritance-and-polymorphism/README.md) |
4343
| templates-basics | [C++](languages/cpp/03-advanced/templates-basics/README.md) | [C#](languages/csharp/03-advanced/templates-basics/README.md) | [Go](languages/go/03-advanced/templates-basics/README.md) | Planned | [Python](languages/python/03-advanced/templates-basics/README.md) | [TypeScript](languages/typescript/03-advanced/templates-basics/README.md) |
4444

4545
### 04-expert

LANGUAGE_PARITY_MATRIX.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Current parity progress in non-C++ tracks:
4747

4848
- C#: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
4949
- Go: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
50-
- Java: `2/5` modules complete in `03-advanced`, `0/5` in `04-expert`
50+
- Java: `3/5` modules complete in `03-advanced`, `0/5` in `04-expert`
5151
- Python: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
5252
- TypeScript: `5/5` modules complete in `03-advanced`, `5/5` in `04-expert`
5353

@@ -58,7 +58,7 @@ Current parity progress in non-C++ tracks:
5858
| 1 | structs-and-classes | Done | Done | Done | Done | Done | Done |
5959
| 2 | constructors-and-invariants | Done | Done | Done | Done | Done | Done |
6060
| 3 | copy-and-move-semantics | Done | Done | Done | Planned | Done | Done |
61-
| 4 | inheritance-and-polymorphism | Done | Done | Done | Planned | Done | Done |
61+
| 4 | inheritance-and-polymorphism | Done | Done | Done | Done | Done | Done |
6262
| 5 | templates-basics | Done | Done | Done | Planned | Done | Done |
6363

6464
`04-expert` projects and assessments are now implemented across all active non-Java tracks.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ If you only want to learn one track, follow that track README first because lang
5656
| C++ | 00-setup, 01-foundations, 02-core, 03-advanced, 04-expert | Foundations, Core, Advanced, Expert, projects, assessments | Most complete and primary track |
5757
| C# | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
5858
| Go | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
59-
| Java | 01-foundations, 02-core, 03-advanced partial | 8/8 foundations modules, 6/6 core modules, 2/5 advanced modules, 2/4 projects, 2/4 assessments | Java 21 MVP through core plus two advanced modules; remaining advanced, expert, and later checkpoints planned |
59+
| Java | 01-foundations, 02-core, 03-advanced partial | 8/8 foundations modules, 6/6 core modules, 3/5 advanced modules, 2/4 projects, 2/4 assessments | Java 21 MVP through core plus three advanced modules; remaining advanced, expert, and later checkpoints planned |
6060
| Python | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
6161
| TypeScript | 01-foundations, 02-core, 03-advanced, 04-expert | 8/8 foundations modules, 6/6 core modules, 5/5 advanced modules, 5/5 expert modules, 4/4 projects, 4/4 assessments | Module and checkpoint parity complete through expert |
6262

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
# Java 03 Advanced
22

3-
This level starts the advanced Java track with object modeling and constructor-enforced invariants. It is currently a partial expansion.
3+
This level starts the advanced Java track with object modeling, constructor-enforced invariants, and polymorphism. It is currently a partial expansion.
44

55
## Learning Metadata
66

77
- Difficulty: Intermediate.
8-
- Estimated Time: 1.5-2 hours for the current modules.
8+
- Estimated Time: 2-3 hours for the current modules.
99
- Prerequisites: Completion of `01-foundations` and `02-core`.
1010
- Study Strategy: Focus on data plus behavior first, then compare Java classes with records, structs, and classes in other tracks.
1111

1212
## Module Order
1313

1414
1. [structs-and-classes](./structs-and-classes/README.md)
1515
2. [constructors-and-invariants](./constructors-and-invariants/README.md)
16+
3. [inheritance-and-polymorphism](./inheritance-and-polymorphism/README.md)
1617

1718
## Level Outcomes
1819

1920
- Model related data and behavior with Java records and classes.
2021
- Protect object state through constructors and methods.
2122
- Define invariants that remain true after construction and updates.
23+
- Treat different concrete classes through one shared Java interface.
2224
- Compare Java object modeling with C++, C#, Go, Python, and TypeScript.
2325

2426
## Done When
@@ -27,10 +29,13 @@ This level starts the advanced Java track with object modeling and constructor-e
2729
- [ ] I completed both `structs-and-classes` exercises.
2830
- [ ] I ran `constructors-and-invariants/example/Main.java`.
2931
- [ ] I completed both `constructors-and-invariants` exercises.
32+
- [ ] I ran `inheritance-and-polymorphism/example/Main.java`.
33+
- [ ] I completed both `inheritance-and-polymorphism` exercises.
3034
- [ ] I can explain when a Java record is enough and when a class is better.
3135
- [ ] I can keep state updates behind methods instead of editing fields directly.
3236
- [ ] I can explain how constructors protect invariants.
37+
- [ ] I can explain how interfaces enable polymorphic collections.
3338

3439
## Study Tip
3540

36-
This level is intentionally partial. Finish these object-state modules before expanding into copy/reference behavior, inheritance, and generics.
41+
This level is intentionally partial. Finish these object-state and polymorphism modules before expanding into copy/reference behavior and generics.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Inheritance and Polymorphism (Java)
2+
3+
This module models behavior variation with interfaces and overriding implementations.
4+
5+
## Learning Metadata
6+
7+
- Difficulty: Intermediate.
8+
- Estimated Time: 35-50 minutes.
9+
- Prerequisites: `03-advanced/structs-and-classes`, `03-advanced/constructors-and-invariants`.
10+
- Cross-Language Lens: Compare virtual dispatch, interfaces, and duck-typed behavior to see how polymorphism changes by language.
11+
12+
## Quick Run
13+
14+
~~~bash
15+
javac -d build/java languages/java/03-advanced/inheritance-and-polymorphism/example/Main.java
16+
java -cp build/java Main
17+
~~~
18+
19+
## Topics Covered
20+
21+
- Interfaces and required methods.
22+
- Runtime polymorphism through shared interface references.
23+
- Overriding concrete behavior in implementing classes.
24+
- Aggregating mixed implementations in one list.
25+
26+
## Common Pitfalls
27+
28+
- Forgetting to implement required interface methods.
29+
- Accessing implementation-only state from generic iteration code.
30+
- Replacing polymorphism with `instanceof` checks.
31+
32+
## Cross-Language Notes
33+
34+
- Java interfaces define named contracts that classes explicitly implement.
35+
- Compared with Go, Java uses nominal interface implementation; compared with Python, more mistakes are caught before execution.
36+
- The key lesson is that polymorphism is a design idea first, not just a language feature.
37+
38+
## Exercise Focus
39+
40+
- exercises/Exercise01.java: extend shape model with `Triangle`.
41+
- exercises/Exercise02.java: aggregate shapes in `List<Shape>`.
42+
43+
### Exercise Specs
44+
45+
1. exercises/Exercise01.java
46+
- Input: triangle base and height.
47+
- Output: computed area through overridden method.
48+
- Edge cases: zero dimensions; decimal dimensions.
49+
50+
2. exercises/Exercise02.java
51+
- Input: predefined shape objects.
52+
- Output: total area through polymorphic iteration.
53+
- Edge cases: empty shape list; mixed shape types.
54+
55+
## Checkpoint
56+
57+
- [ ] I can define interfaces with polymorphic methods.
58+
- [ ] I can override behavior in implementing classes.
59+
- [ ] I can use polymorphic collections safely.
60+
- [ ] I completed exercises/Exercise01.java.
61+
- [ ] I completed exercises/Exercise02.java.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Module focus: Treating different concrete types through one common interface.
2+
// Why it matters: practicing inheritance and polymorphism patterns makes exercises and checkpoints easier to reason about.
3+
4+
import java.util.List;
5+
import java.util.Locale;
6+
7+
public class Main {
8+
interface Shape {
9+
// The interface names the behavior generic code can rely on.
10+
double area();
11+
12+
String name();
13+
}
14+
15+
static class Rectangle implements Shape {
16+
private final double width;
17+
private final double height;
18+
19+
Rectangle(double width, double height) {
20+
this.width = width;
21+
this.height = height;
22+
}
23+
24+
@Override
25+
public double area() {
26+
// Each concrete class owns its formula behind the shared interface.
27+
return width * height;
28+
}
29+
30+
@Override
31+
public String name() {
32+
return "Rectangle";
33+
}
34+
}
35+
36+
static class Circle implements Shape {
37+
private final double radius;
38+
39+
Circle(double radius) {
40+
this.radius = radius;
41+
}
42+
43+
@Override
44+
public double area() {
45+
// Callers do not need to know this formula when they use Shape references.
46+
return Math.PI * radius * radius;
47+
}
48+
49+
@Override
50+
public String name() {
51+
return "Circle";
52+
}
53+
}
54+
55+
public static void main(String[] args) {
56+
Locale.setDefault(Locale.US);
57+
58+
// Prepare sample inputs that exercise the key inheritance and polymorphism path.
59+
List<Shape> shapes = List.of(new Rectangle(3.0, 4.0), new Circle(2.0));
60+
61+
for (Shape shape : shapes) {
62+
// Report values so learners can verify the inheritance and polymorphism outcome.
63+
System.out.printf("%s area: %.2f%n", shape.name(), shape.area());
64+
}
65+
}
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Locale;
2+
import java.util.Scanner;
3+
4+
public class Exercise01 {
5+
interface Shape {
6+
double area();
7+
}
8+
9+
static class Triangle implements Shape {
10+
private final double base;
11+
private final double height;
12+
13+
Triangle(double base, double height) {
14+
this.base = base;
15+
this.height = height;
16+
}
17+
18+
@Override
19+
public double area() {
20+
return 0.5 * base * height;
21+
}
22+
}
23+
24+
public static void main(String[] args) {
25+
Locale.setDefault(Locale.US);
26+
Scanner scanner = new Scanner(System.in);
27+
if (!scanner.hasNextDouble()) {
28+
System.out.println("Invalid input.");
29+
return;
30+
}
31+
double base = scanner.nextDouble();
32+
33+
if (!scanner.hasNextDouble()) {
34+
System.out.println("Invalid input.");
35+
return;
36+
}
37+
double height = scanner.nextDouble();
38+
39+
if (base <= 0.0 || height <= 0.0) {
40+
System.out.println("Base and height must be positive.");
41+
return;
42+
}
43+
44+
Shape triangle = new Triangle(base, height);
45+
System.out.printf("Triangle area: %.2f%n", triangle.area());
46+
}
47+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.List;
2+
import java.util.Locale;
3+
4+
public class Exercise02 {
5+
interface Shape {
6+
double area();
7+
}
8+
9+
static class Rectangle implements Shape {
10+
private final double width;
11+
private final double height;
12+
13+
Rectangle(double width, double height) {
14+
this.width = width;
15+
this.height = height;
16+
}
17+
18+
@Override
19+
public double area() {
20+
return width * height;
21+
}
22+
}
23+
24+
static class Circle implements Shape {
25+
private final double radius;
26+
27+
Circle(double radius) {
28+
this.radius = radius;
29+
}
30+
31+
@Override
32+
public double area() {
33+
return Math.PI * radius * radius;
34+
}
35+
}
36+
37+
public static void main(String[] args) {
38+
Locale.setDefault(Locale.US);
39+
List<Shape> shapes = List.of(new Rectangle(2.0, 5.0), new Circle(1.5));
40+
41+
double totalArea = 0.0;
42+
for (Shape shape : shapes) {
43+
totalArea += shape.area();
44+
}
45+
46+
System.out.printf("Total area: %.2f%n", totalArea);
47+
}
48+
}

languages/java/CHECKLIST.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747
- [ ] structs-and-classes exercises
4848
- [ ] constructors-and-invariants example
4949
- [ ] constructors-and-invariants exercises
50+
- [ ] inheritance-and-polymorphism example
51+
- [ ] inheritance-and-polymorphism exercises
5052

5153
## Planned
5254

5355
- [ ] copy-and-move-semantics module
54-
- [ ] inheritance-and-polymorphism module
5556
- [ ] templates-basics module
5657
- [ ] 04-expert modules
5758
- [ ] projects/03-advanced

languages/java/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This track currently covers `01-foundations` and `02-core` as a Java 21 MVP, inc
66

77
- 8/8 foundations modules implemented.
88
- 6/6 core modules implemented.
9-
- 2/5 advanced modules implemented (`structs-and-classes`, `constructors-and-invariants`).
9+
- 3/5 advanced modules implemented (`structs-and-classes`, `constructors-and-invariants`, `inheritance-and-polymorphism`).
1010
- 0/5 expert modules implemented.
1111
- 2/4 projects implemented (`01-foundations`, `02-core`).
1212
- 2/4 assessments implemented (`01-foundations`, `02-core`).
@@ -52,6 +52,7 @@ java -cp build/java Main
5252
- [03-advanced](./03-advanced/README.md)
5353
- [structs-and-classes](./03-advanced/structs-and-classes/README.md)
5454
- [constructors-and-invariants](./03-advanced/constructors-and-invariants/README.md)
55+
- [inheritance-and-polymorphism](./03-advanced/inheritance-and-polymorphism/README.md)
5556
- Projects
5657
- [01-foundations](./projects/01-foundations/README.md)
5758
- [02-core](./projects/02-core/README.md)

0 commit comments

Comments
 (0)