Spaces:
Running
Running
public, protected, no set level, private
Browse files
Week 5: Class hierarchies/05A. Encapsulation and inheriting [++Attribute's 4 visibility levels]
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The child class inherits all the traits of the parent class.
|
| 2 |
+
However, the child class does NOT have DIRECT ACCESS to the 'features of the parent class that are protected by the private wrapper'.
|
| 3 |
+
|
| 4 |
+
If an attempt is made to reference the Name attribute of the Person attribute of the Student class, a translation error occurs:
|
| 5 |
+
|
| 6 |
+
class Student extends Person {
|
| 7 |
+
private int studypoints;
|
| 8 |
+
|
| 9 |
+
public Student(String name, String email, int studypoints) {
|
| 10 |
+
this.name = name; // HERE
|
| 11 |
+
this.studypoints = studypoints;
|
| 12 |
+
}
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
However, it would often be convenient to refer to attributes in a child class.
|
| 17 |
+
But if the private wrapper is removed, the client also has direct access to modify the attributes.
|
| 18 |
+
So how to solve the dilemma?
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
============
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
Visibility level protected
|
| 27 |
+
In Java, the problem is solved by a 'third visibility matrix'.
|
| 28 |
+
In addition to private and public, the visibility wrapper can be set to protected.
|
| 29 |
+
|
| 30 |
+
A feature protected by the protected wrapper is
|
| 31 |
+
- is AVAILABLE in the CHILD class but
|
| 32 |
+
- is NOT VISIBLE to CLIENTS of the class
|
| 33 |
+
|
| 34 |
+
The table below summarizes all 4 visibility definitions in Java
|
| 35 |
+
(including the omission of a visibility wrapper as a visibility definition in Java):
|
| 36 |
+
|
| 37 |
+
Visibility levels in Java
|
| 38 |
+
Level Visible within class Visible within package Visible within CHILD classes Visible everywhere
|
| 39 |
+
public yes yes yes yes
|
| 40 |
+
protected yes yes yes no <= HERE
|
| 41 |
+
no set level yes yes no no
|
| 42 |
+
private yes no no no
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
Let's now change the definition of the Person class so that the visibility of attributes is 'protected' instead of 'private':
|
| 48 |
+
|
| 49 |
+
class Person {
|
| 50 |
+
// 'protected' attribute - subclasses can directly access, clients cant directly access
|
| 51 |
+
protected String name;
|
| 52 |
+
protected String email;
|
| 53 |
+
|
| 54 |
+
public Person(String name, String email) {
|
| 55 |
+
this.name = name;
|
| 56 |
+
this.email = email;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
public String getName() {
|
| 60 |
+
return name;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
public void setName(String name) {
|
| 64 |
+
this.name = name;
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
public String getEmail() {
|
| 68 |
+
return email;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
public void setEmail(String email) {
|
| 72 |
+
this.email= email;
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
Now the Student class can use inherited attributes.
|
| 78 |
+
The method printStudent refers directly to the attributes defined in the parent class Person.
|
| 79 |
+
|
| 80 |
+
class Student extends Person {
|
| 81 |
+
private int studypoints;
|
| 82 |
+
|
| 83 |
+
public Student(String name, String email, int studypoints) {
|
| 84 |
+
//HERE
|
| 85 |
+
super(name, email);
|
| 86 |
+
this.studypoints = studypoints;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
public void printStudent() {
|
| 90 |
+
// Now in the child class we can refer straight to
|
| 91 |
+
// the attributes defined in the parent class
|
| 92 |
+
System.out.println("Name" + name);
|
| 93 |
+
System.out.println("Email:" + email);
|
| 94 |
+
System.out.println("Studypoints: " + studypoints);
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
public int getStudypoints() {
|
| 98 |
+
return studypoints;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
public void setStudypoints(int studypoints) {
|
| 102 |
+
this.studypoints = studypoints;
|
| 103 |
+
}
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|