KaiquanMah commited on
Commit
2ac5973
·
verified ·
1 Parent(s): a6d151f

MULTIPLE CONSTRUCTORS, EMPTY CONSTRUCTOR

Browse files
Week 5: Class hierarchies/11A. About parent class constructor++ ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ TYPICALLY, METHOD OVERRIDING is used IN CONSTRUCTORS.
3
+ As an example, consider the class Account, which now has 3 different constructors.
4
+ This allows the client to easily create a creature even in a situation "where not all the data content is known":
5
+
6
+ class Account {
7
+ private String accnumber;
8
+ private String owner;
9
+ private double balance;
10
+
11
+ public Account(String accnumber, String owner, double balance) {
12
+ this.accnumber = accnumber;
13
+ this.owner = owner;
14
+ this.balance = balance;
15
+ }
16
+
17
+ public Account(String accnumber, String owner) {
18
+ this.accnumber = accnumber;
19
+ this.owner = owner;
20
+ this.balance = 0.0;
21
+ }
22
+
23
+ public Account() {
24
+ this.accnumber = "";
25
+ this.owner = "";
26
+ this.balance = 0.0;
27
+ }
28
+ }
29
+
30
+
31
+
32
+ Now an object can be created by calling any of the constructor versions:
33
+ public static void main(String[] args) {
34
+ Account acc1 = new Account();
35
+ Account acc2 = new Account("12345", "Randy Rich");
36
+ Account acc3 = new Account("54321", "Sally Student", 9.40);
37
+ }
38
+
39
+
40
+
41
+
42
+
43
+ In fact, whenever an entity is formed from a child class, a constructor is called from the parent class.
44
+ IF the programmer DOES NOT CALL the PARENT CLASS CONSTRUCTOR itself using the 'super' keyword,
45
+ Java AUTO CALLS an "empty" CONSTRUCTOR (i.e. a constructor method with NO PARAMS).
46
+ If there is no such constructor, the program gives an error.
47
+
48
+ The constructor's job is, among other things,
49
+ to ALLOCATE ENOUGH MEMORY for the object and
50
+ to INITIALISE the ATTRIBUTES of the object, and for this reason all parent class constructors must be called.
51
+
52
+
53
+ class Dog {
54
+ protected String name;
55
+
56
+ public Dog (String name) {
57
+ this.name = name;
58
+ }
59
+ }
60
+
61
+
62
+ class SuperDog extends Dog{
63
+ protected String superpower;
64
+
65
+ // Constructor throws an error now since there is no keyword super
66
+ // and parent class Dog does not contain a constructor with attribute "superpower"
67
+ public SuperDog(String name, String superpower) {
68
+ this.name = name;
69
+ this.superpower = superpower;
70
+ }
71
+ }
72
+
73
+ Program throws a translation error:
74
+ Implicit super constructor Dog() is undefined.
75
+ Must explicitly invoke another constructor
76
+
77
+
78
+ SOL1
79
+ The problem can be fixed either by calling a constructor yourself...
80
+ class SuperDog extends Dog{
81
+ protected String superpower;
82
+
83
+ public SuperDog(String name, String superpower) {
84
+ super(name);
85
+ this.superpower = superpower;
86
+ }
87
+ }
88
+
89
+
90
+ SOL2
91
+ ...or by adding a constructor to the parent class that has no formal parameters.
92
+
93
+ class Dog {
94
+ protected String name;
95
+
96
+ //ADD EMPTY CONSTRUCTOR WO PARAMS
97
+ public Dog() {
98
+ name = "No name.";
99
+ }
100
+
101
+ public Dog(String name) {
102
+ this.name = name;
103
+ }
104
+ }
105
+
106
+ class SuperDog extends Dog{
107
+ protected String superpower;
108
+
109
+ // Now no error pops up, because parent class has
110
+ // a constructor you can call this way: super();
111
+ public SuperDog(String name, String supwerpower) {
112
+ this.name = name;
113
+ this.superpower = superpower;
114
+ }
115
+ }
116
+
117
+
118
+
119
+ Note that an "empty" constructor is also 'a class for which no constructor is defined'.
120
+ class Dog {
121
+ protected String name;
122
+
123
+ //NO CONSTRUCTOR
124
+ }
125
+
126
+ public class Dogtest {
127
+ public static void main(String[] args) {
128
+ // Works, because Java adds a "default constructor"
129
+ Dog dog = new Dog();
130
+ }
131
+ }
132
+
133
+
134
+
135
+
136
+ However, if the programmer adds a NEW OVERRIDING CONSTRUCTOR to the class itself, there is no longer an empty constructor.
137
+
138
+ So the next program will again give an error:
139
+ class Dog {
140
+ protected String name;
141
+
142
+ //CONSTRUCTOR
143
+ public Dog(String name) {
144
+ this.name = name;
145
+ }
146
+ }
147
+
148
+ public class Dogtest {
149
+ public static void main(String[] args) {
150
+ // Doesn't work, since own constructor
151
+ // deletes PREVIOUS/default constructor
152
+
153
+ // AND IN THE NEW USER'S OVERRIDING CONSTRUCTOR
154
+ // THERE IS NO EMPTY CONSTRUCTOR
155
+ // SO THERE WILL BE 'TRANSLATION ERROR'
156
+ Dog dog = new Dog();
157
+ }
158
+ }
159
+
160
+ Program throws a translation error:
161
+ Implicit super constructor Dog() is undefined.
162
+ Must explicitly invoke another constructor
163
+
164
+
165
+