-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch_49_dynamic_method.java
50 lines (38 loc) · 1.19 KB
/
ch_49_dynamic_method.java
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
class Phone {
int i = 10;
public void showTime() {
System.out.println("Time is 8 am");
}
public void on() {
System.out.println("Turning on Phone...");
}
}
class SmartPhone extends Phone {
int i = 20;
public void music() {
System.out.println("Playing music...");
}
public void on() {
System.out.println("Turning on SmartPhone...");
}
}
public class ch_49_dynamic_method {
public static void main(String[] args) {
// Phone obj = new Phone(); // Allowed
// SmartPhone smobj = new SmartPhone(); // Allowed
// obj.name();
Phone obj = new SmartPhone(); // Yes it is allowed
// SmartPhone obj2 = new Phone(); // Not allowed
obj.showTime(); // only methods from base can be called
obj.on(); // overridden method is called
// obj.music(); // Not Allowed
System.out.println(obj.i);
}
}
/*
* here 'Phone obj' means only reference is created. now 'Phone obj = new
SmartPhone();' means object is created for base class Smartphone, only
reference is made for 'Phone'.
* we cannot access methods of class Smartphone because
data members cannot be overridden.
*/