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