Last change
on this file was 1349, checked in by wouter, 5 days ago |
#415 added test showing problem with missing parent init
|
File size:
918 bytes
|
Line | |
---|
1 | package testcode;
|
---|
2 |
|
---|
3 | class Parent1 {
|
---|
4 | private String x;
|
---|
5 |
|
---|
6 | public Parent1() {
|
---|
7 | this.x = "ok1";//default
|
---|
8 | }
|
---|
9 |
|
---|
10 | public String toString() {
|
---|
11 | return x;
|
---|
12 | }
|
---|
13 | }
|
---|
14 |
|
---|
15 | class Child1 extends Parent1 {
|
---|
16 |
|
---|
17 | }
|
---|
18 |
|
---|
19 | class Parent2 {
|
---|
20 | private String x;
|
---|
21 |
|
---|
22 | public Parent2(String x) {
|
---|
23 | this.x = x;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public String toString() {
|
---|
27 | return x;
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | class Child2 extends Parent2 {
|
---|
32 |
|
---|
33 | public Child2() {
|
---|
34 | super("ok2");
|
---|
35 | }
|
---|
36 |
|
---|
37 | }
|
---|
38 |
|
---|
39 | // test #415
|
---|
40 | class ParentThatNeedsInit {
|
---|
41 |
|
---|
42 | private String value;
|
---|
43 |
|
---|
44 | public ParentThatNeedsInit() {
|
---|
45 | this.value = "ok3";
|
---|
46 | }
|
---|
47 |
|
---|
48 | public String getValue() {
|
---|
49 | return value;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | class ChildOfPTNI extends ParentThatNeedsInit {
|
---|
54 | public ChildOfPTNI() {
|
---|
55 | // in java, parent constructor is called implicitly
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public class Constructor {
|
---|
60 |
|
---|
61 | public static void main(String[] args) {
|
---|
62 | System.out.println(new Child1().toString());
|
---|
63 | System.out.println(new Child2().toString());
|
---|
64 | System.out.println(new ChildOfPTNI().getValue());
|
---|
65 | }
|
---|
66 |
|
---|
67 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.