KaiquanMah commited on
Commit
cb12565
·
verified ·
1 Parent(s): 5209f8c

setParentAttribute(value1) OR super(value1, ...)

Browse files
Week 5: Class hierarchies/03A. Adding features to child class ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ The idea of inheritance is usually to ADD NEW FEATURES to SUBCLASSES,
2
+ i.e. to SPECIALISE the activities of classes.
3
+ Let's continue developing the 'Student' class by writing a constructor for it,
4
+ which will have a name and an email address as well as a number of credits as parameters.
5
+
6
+
7
+ In addition, write setting and observation methods for the number of credits:
8
+ class Student extends Person {
9
+ // new attribute, that person does not have
10
+ private int studypoints;
11
+
12
+ //CONSTRUCTOR
13
+ public Student(String name, String email, int studypoints) {
14
+ // Setting attributes from parents class
15
+ // by CALLING METHODS INHERITED from it
16
+ setName(name);
17
+ setEmail(email);
18
+
19
+ // child class's "own" attribute
20
+ this.studypoints = studypoints;
21
+ }
22
+
23
+ public int getStudypoints() {
24
+ return studypoints;
25
+ }
26
+
27
+ public void setStudypoints(int studypoints) {
28
+ this.studypoints = studypoints;
29
+ }
30
+ }
31
+
32
+
33
+ Now we can form an object from the Student class:
34
+ Student sam = new Student("Sam Student", "sam@example.com", 123);
35
+ System.out.println(sam.getStudypoints());
36
+
37
+
38
+ Program outputs:
39
+ 123
40
+
41
+
42
+
43
+
44
+
45
+ Since 'Student' inherits the class 'Person', the features of the 'Person' class are also available.
46
+ The 'Student' class instance can therefore also be used to call the
47
+ name and email configuration and observation methods, for example:
48
+
49
+ Student sam = new Student("Sam Student", "sam@example.com", 123);
50
+ System.out.println(sam.getStudypoints());
51
+ System.out.println(sam.getName());
52
+ System.out.println(sam.getEmail());
53
+
54
+
55
+ Program outputs:
56
+ 123
57
+ Sam Student
58
+ sam@example.com
59
+
60
+
61
+
62
+
63
+
64
+
65
+ Let's also implement the constructor and new setting and observation methods in the Teacher class:
66
+ class Teacher extends Person {
67
+ private int courses;
68
+
69
+ public Teacher(String name, String email, int courses) {
70
+ setName(name);
71
+ setEmail(email);
72
+ this.courses = courses;
73
+ }
74
+
75
+ public int getCourses() {
76
+ return courses;
77
+ }
78
+
79
+ public void setCourses(int courses) {
80
+ this.courses = courses;
81
+ }
82
+ }
83
+
84
+
85
+ Now the teacher and the pupil have not only common characteristics, but also their own:
86
+
87
+ Student sam = new Student("Sam Student", "sam@example.com", 123);
88
+ Teacher tina = new Teacher("Tina Teacher", "tina@example.com", 4);
89
+
90
+ // both have a name and an email
91
+ System.out.println(sam.getName());
92
+ System.out.println(sam.getEmail());
93
+
94
+ System.out.println(tina.getName());
95
+ System.out.println(tina.getEmail());
96
+
97
+ // only student has study points
98
+ System.out.println(sam.getStudypoints());
99
+
100
+ // only teacher has course amount
101
+ System.out.println(tina.getCourses());
102
+
103
+
104
+ Program outputs:
105
+ Sam Student
106
+ sam@example.com
107
+ Tina Teacher
108
+ tina@example.com
109
+ 123
110
+ 4
111
+
112
+
113
+ =================================
114
+
115
+
116
+
117
+
118
+ Calling 'parent class constructor'
119
+ In the previous examples, the values inherited from the parent class were set using the setting methods.
120
+ However, it would be easier to 'CALL the PARENT CLASS CONSTRUCTOR DIRECTLY, especially if there are MANY VALUES TO SET'.
121
+
122
+ This can be done with the 'super' keyword. It can be used to refer from a child class to a parent class.
123
+
124
+ Let's start by writing a constructor for the 'Person' class to set the values of the attributes:
125
+ // PARENT CLASS
126
+ class Person {
127
+ private String name;
128
+ private String email;
129
+
130
+ public Person (String name, String email) {
131
+ this.name = name;
132
+ this.email = email;
133
+ }
134
+
135
+ public String getName() {
136
+ return name;
137
+ }
138
+
139
+ public void setName(String name) {
140
+ this.name = name;
141
+ }
142
+
143
+ public String getEmail() {
144
+ return email;
145
+ }
146
+
147
+ public void setEmail(String email) {
148
+ this.email = email;
149
+ }
150
+ }
151
+
152
+
153
+
154
+
155
+
156
+ Then modify the Student class so that its constructor calls the parent class constructor:
157
+ class Student extends Person {
158
+ // new attribute that the person does not have
159
+ private int studypoints;
160
+
161
+ public Student(String name, String email, int studypoints) {
162
+ super(name, email);
163
+ this.studypoints = studypoints;
164
+ }
165
+
166
+ public int getStudypoints() {
167
+ return studypoints;
168
+ }
169
+
170
+ public void setStudypoints(int studypoints) {
171
+ this.studypoints = studypoints;
172
+ }
173
+ }
174
+
175
+
176
+
177
+
178
+
179
+
180
+ Let's make a similar change to the Teacher category:
181
+ class Teacher extends Person {
182
+ private int courses;
183
+
184
+ public Teacher(String name, String email, int courses) {
185
+ super(name, email);
186
+ this.courses = courses;
187
+ }
188
+
189
+ public int getCourses() {
190
+ return courses;
191
+ }
192
+
193
+ public void setCourses(int courses) {
194
+ this.courses = courses;
195
+ }
196
+ }
197
+
198
+