-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.java
61 lines (50 loc) · 3.59 KB
/
Example.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
51
52
53
54
55
56
57
58
59
60
61
package com.trl.theoreticalKnowledge.inheritance.variable.a.a1;
import static java.lang.System.out;
public class Example {
public static void main(String[] args) {
Parent parent = new Parent();
// out.println(parent.someValue_1); // Compilation ERROR !!! Access Modifier is private.
out.println(parent.someValue_2); // Output: P
out.println(parent.someValue_3); // Output: P
out.println(parent.someValue_4); // Output: P
out.println(parent.someValue_5); // Output: P
out.println(parent.someValue_6); // Not ERROR !!! Compiler look for the type of reference.
out.println(parent.someValue_7); // Not ERROR !!! Compiler look for the type of reference.
out.println();
Child child = new Child();
// out.println(child.someValue_1); // Compilation ERROR !!! Access Modifier is private.
out.println(child.someValue_2); // Output: C
out.println(child.someValue_3); // Output: C
out.println(child.someValue_4); // Output: C
out.println(child.someValue_5); // Output: C
out.println(child.someValue_6); // Not ERROR !!! Compiler look for the type of reference.
out.println(child.someValue_7); // Not ERROR !!! Compiler look for the type of reference.
out.println();
Parent parent_child = new Child();
// out.println(parent_child.someValue_1); // Compilation ERROR !!! Access Modifier is private.
out.println(parent_child.someValue_2); // Output: P. Variables is not have overriding. Variables is not virtual.
out.println(parent_child.someValue_3); // Output: P. Variables is not have overriding. Variables is not virtual.
out.println(parent_child.someValue_4); // Output: P. Variables is not have overriding. Variables is not virtual.
out.println(parent_child.someValue_5); // Output: P. Variables is not have overriding. Variables is not virtual.
out.println(parent_child.someValue_6); // Not ERROR !!! Compiler look for the type of reference.
out.println(parent_child.someValue_7); // Not ERROR !!! Compiler look for the type of reference.
}
}
class Parent {
private String someValue_1 = "P";
String someValue_2 = "P";
protected String someValue_3 = "P";
public String someValue_4 = "P";
public final String someValue_5 = "P";
public static String someValue_6 = "P";
public static final String someValue_7 = "P";
}
class Child extends Parent {
private String someValue_1 = "C";
String someValue_2 = "C";
protected String someValue_3 = "C";
public String someValue_4 = "C";
public final String someValue_5 = "C";
public static String someValue_6 = "C";
public static final String someValue_7 = "C";
}