Skip to content

Commit a4d6ca4

Browse files
feat: added flexible constructor bodies
1 parent dd8106b commit a4d6ca4

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.claudioaltamura.java23.flexible_constructor_bodies;
2+
3+
public class Child extends Parent {
4+
5+
private final int b;
6+
7+
public Child(int a, int b) {
8+
//this is now allowed!
9+
if(a < 0) throw new IllegalArgumentException("a must be => 0");
10+
if(b < 0) throw new IllegalArgumentException("b must be => 0");
11+
this.b = b;
12+
super(a);
13+
}
14+
15+
public int getB() {
16+
return b;
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package de.claudioaltamura.java23.flexible_constructor_bodies;
2+
3+
public class Parent {
4+
5+
private final int a;
6+
7+
public Parent(int a) {
8+
this.a = a;
9+
}
10+
11+
public int getA() {
12+
return a;
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.claudioaltamura.java23.flexible_constructor_bodies;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class ChildTest {
8+
9+
@Test
10+
void shouldCreateAChild() {
11+
assertDoesNotThrow(() -> new Child(1,2));
12+
}
13+
14+
@Test
15+
void shouldNotCreateAChild() {
16+
assertThrows(IllegalArgumentException.class, () -> new Child(-1,2));
17+
}
18+
19+
20+
}

0 commit comments

Comments
 (0)