You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: oop.md
+44-5Lines changed: 44 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -501,7 +501,7 @@ class Implementation implements A, B {
501
501
**Q:** What happens if a class implements two interfaces with conflicting default methods?
502
502
**A:** Compile error — the class must override the conflicting method to explicitly resolve which implementation to use or provide its own.
503
503
504
-
## 🃏Nested Classes vs Inner Classes - Key Distinctions
504
+
## 🃏Nested Classes vs Inner Classes - Key Distinctions
505
505
506
506
**Rule:** Inner class = NON-STATIC nested class. All inner classes are nested, but not all nested classes are inner.
507
507
@@ -514,6 +514,8 @@ class Implementation implements A, B {
514
514
* Local inner class
515
515
* Anonymous inner class
516
516
517
+
**Important:** Classes inside interfaces, nested enums, and nested records are **implicitly static** (not inner classes). The "static" keyword only applies to nested types, never top-level types.
// record MyRecord() { } // Always implicitly static - cannot be inner!
632
671
}
633
672
634
-
// Pitfall 4: Static nested accessing instance members
673
+
// Pitfall 5: Static nested accessing instance members
635
674
classExample {
636
675
String field ="test";
637
676
staticclassBadNested {
@@ -642,7 +681,7 @@ class Example {
642
681
}
643
682
```
644
683
645
-
**💡 Learning Tip:** "Static nested = NO instance access, Inner = FULL access" - Static nested classes can't access instance members, while inner classes can access everything in the outer class.
684
+
**💡 Learning Tip:** "Static nested = NO instance access, Inner = FULL access" - Static nested classes (including implicitly static enums/records/interface classes) can't access instance members, while inner classes can access everything in the outer class. Remember: "static" keyword only applies to nested types, never top-level types.
646
685
647
686
**Q:** What's the key difference between a static nested class and an inner class?
648
-
**A:** A static nested class cannot access non-static members of the outer class and doesn't need an outer instance to be created, while an inner class (non-static nested) can access all outer class members and requires an outer instance.
687
+
**A:** A static nested class cannot access non-static members of the outer class and doesn't need an outer instance to be created, while an inner class (non-static nested) can access all outer class members and requires an outer instance. Enums, records, and classes inside interfaces are always implicitly static.
0 commit comments