Skip to content

Commit 1bec4ac

Browse files
committed
Rev Notes: 🟥 Member Inner Classes
1 parent 7d8362d commit 1bec4ac

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

src/chapter_1/revision_notes/README.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,77 @@ public enum Seasons {
7979
abstract void printHours();
8080
}
8181
```
82-
* We can also remove the `abstract` modifier so that all of the enums will use a default implementation.
82+
* We can also remove the `abstract` modifier so that all of the enums will use a default implementation.
83+
84+
<hr>
85+
86+
# 🧠 1.5 Creating Nested Classes
87+
88+
## 🟥 Member Inner Classes
89+
* A member inner class can have any access modifier, it can extend any class/interface (including other inner classes)
90+
* Can be abstract or final, they can not contain static members
91+
* They CAN access enclosing class's members (even private ones)
92+
```java
93+
public class Outer {
94+
private String greeting ="hi";
95+
private class Inner {
96+
public void go() {
97+
for (int i=0;i<3;i++)
98+
System.out.println(greeting);
99+
}
100+
}
101+
void callInner() {
102+
Inner inner = new Inner();
103+
}
104+
public static void main(String[] args) {
105+
// Inner inner = new Inner(); // COMPILER ERROR
106+
new Outer().callInner();;
107+
}
108+
}
109+
public class Outer {
110+
private String greeting ="hi";
111+
private class Inner {
112+
public void go() {
113+
for (int i=0;i<3;i++)
114+
System.out.println(greeting);
115+
}
116+
}
117+
void callInner() {
118+
Inner inner = new Inner();
119+
}
120+
public static void main(String[] args) {
121+
// Inner inner = new Inner(); // COMPILER ERROR
122+
new Outer().callInner();
123+
Inner inner = new Outer().new Inner();
124+
}
125+
}
126+
```
127+
128+
<br>
129+
130+
* The inner class can have the same variable names as outside:
131+
132+
```java
133+
public class A {
134+
int x = 11;
135+
class B {
136+
int x = 22;
137+
class C {
138+
int x = 33;
139+
int y = 456;
140+
void printAll() {
141+
System.out.println(x); // 33
142+
System.out.println(this.x); //33
143+
System.out.println(B.this.x); // 22
144+
System.out.println(A.this.x); // 11
145+
}
146+
}
147+
}
148+
public static void main(String[] args) {
149+
int aX = new A().x;
150+
int bX = new A().new B().x;
151+
int cX = new A().new B().new C().x;
152+
new A().new B().new C().printAll();
153+
}
154+
}
155+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package chapter_1.revision_notes.memberinnerclass;
2+
3+
public class A {
4+
int x = 11;
5+
class B {
6+
int x = 22;
7+
class C {
8+
int x = 33;
9+
int y = 456;
10+
void printAll() {
11+
System.out.println(x); // 33
12+
System.out.println(this.x); //33
13+
System.out.println(B.this.x); // 22
14+
System.out.println(A.this.x); // 11
15+
}
16+
}
17+
}
18+
public static void main(String[] args) {
19+
int aX = new A().x;
20+
int bX = new A().new B().x;
21+
int cX = new A().new B().new C().x;
22+
new A().new B().new C().printAll();
23+
}
24+
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package chapter_1.revision_notes.memberinnerclass;
2+
3+
public class Outer {
4+
private String greeting ="hi";
5+
private class Inner {
6+
public void go() {
7+
for (int i=0;i<3;i++)
8+
System.out.println(greeting);
9+
}
10+
}
11+
void callInner() {
12+
Inner inner = new Inner();
13+
}
14+
public static void main(String[] args) {
15+
// Inner inner = new Inner(); // COMPILER ERROR
16+
new Outer().callInner();;
17+
Inner inner = new Outer().new Inner();
18+
19+
}
20+
}

0 commit comments

Comments
 (0)