Spaces:
Running
Running
child class method overwrites parent class method
Browse files
Week 5: Class hierarchies/09A. Calling parent class methods [ENCAPSULATION]
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
A child class can call parent class's methods.
|
| 2 |
+
This is useful, for example, when a method is reimplemented in a child class,
|
| 3 |
+
but you want to use the CHILD class IMPLEMENTATION as a BASE.
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
Consider the example of the class Message:
|
| 7 |
+
class Message {
|
| 8 |
+
protected String sender;
|
| 9 |
+
protected String msg;
|
| 10 |
+
|
| 11 |
+
public Message(String sender, String msg) {
|
| 12 |
+
this.sender = sender;
|
| 13 |
+
this.msg = msg;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
public String getMsg() {
|
| 17 |
+
return "Sender: " + sender + "\n\n" + msg;
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
The 'SecretMessage' class inherits the 'Message' class.
|
| 23 |
+
The class has been reimplemented with the method getMsg.
|
| 24 |
+
However, the reimplemented method requests a message from the child/inheriting class only, which is then "encrypted":
|
| 25 |
+
|
| 26 |
+
class SecretMessage extends Message {
|
| 27 |
+
public SecretMessage(String sender, String msg) {
|
| 28 |
+
super(sender, msg);
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Overwritten method, which calls parent class's corresponding
|
| 32 |
+
// method and then "encypts" the message.
|
| 33 |
+
public String getMsg() {
|
| 34 |
+
String content = super.getMsg();
|
| 35 |
+
String secretmsg = "";
|
| 36 |
+
for (int i=0; i<content.length(); i++) {
|
| 37 |
+
// ADD '1' to each character of the string
|
| 38 |
+
// then append to the 'secretmsg' STRING
|
| 39 |
+
secretmsg += (char) (content.charAt(i) + 1);
|
| 40 |
+
}
|
| 41 |
+
return secretmsg;
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
An example of calling classes:
|
| 47 |
+
public static void main(String[] args) {
|
| 48 |
+
Message m = new Message("Pete Public", "Hello.");
|
| 49 |
+
SecretMessage sm = new SecretMessage("Sam Secret", "Hello.");
|
| 50 |
+
|
| 51 |
+
System.out.println("Class message:");
|
| 52 |
+
System.out.println(m.getMsg());
|
| 53 |
+
|
| 54 |
+
System.out.println();
|
| 55 |
+
System.out.println("Class SecretMessage :");
|
| 56 |
+
System.out.println(sm.getMsg());
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
Program outputs:
|
| 61 |
+
Class message:
|
| 62 |
+
Sender: Pete Public
|
| 63 |
+
Hello.
|
| 64 |
+
|
| 65 |
+
Class SecretMessage:
|
| 66 |
+
Ifmmp/
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
Note that CLIENTS of a CHILD class have NO ACCESS to a feature of the PARENT class
|
| 72 |
+
if it has been REIMPLEMENTED (i.e. OVERWRITTEN) in the child class.
|
| 73 |
+
This follows the basic principle of encapsulation: the class decides which features the client has access to.
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
Thus, clients of an entity of type SecretMessage cannot call the method getMsg of the Message class in any way.
|
| 77 |
+
|
| 78 |
+
|