Skip to content

Commit 2eadb2d

Browse files
authored
Update oop.md
1 parent e64bab6 commit 2eadb2d

1 file changed

Lines changed: 147 additions & 1 deletion

File tree

oop.md

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,150 @@ class Implementation implements A, B {
499499
**💡 Learning Tip:** Think "INTERFACE = CONTRACT" - defines what must be done (public methods) but allows flexibility in how (default methods provide optional behavior).
500500

501501
**Q:** What happens if a class implements two interfaces with conflicting default methods?
502-
**A:** Compile error — the class must override the conflicting method to explicitly resolve which implementation to use or provide its own.
502+
**A:** Compile error — the class must override the conflicting method to explicitly resolve which implementation to use or provide its own.
503+
504+
## 🃏Nested Classes vs Inner Classes - Key Distinctions
505+
506+
**Rule:** Inner class = NON-STATIC nested class. All inner classes are nested, but not all nested classes are inner.
507+
508+
**Class Classification Hierarchy:**
509+
* **Top-level class** → Not nested (declared at package level)
510+
* **Nested class** → Declared inside another class/interface body
511+
* **Static nested class** → Explicitly declared static
512+
* **Inner class** → NOT static (non-static nested class)
513+
* Member inner class
514+
* Local inner class
515+
* Anonymous inner class
516+
517+
**Code Examples:**
518+
519+
```java
520+
// Top-level class
521+
public class OuterClass {
522+
private String outerField = "outer";
523+
private static String staticField = "static";
524+
525+
// Static nested class (NOT an inner class)
526+
static class StaticNestedClass {
527+
void display() {
528+
// Can access static members of outer class
529+
System.out.println(staticField); // ✅ Works
530+
// System.out.println(outerField); // ❌ Cannot access non-static
531+
}
532+
}
533+
534+
// Inner class (non-static nested class)
535+
class MemberInnerClass {
536+
void display() {
537+
// Can access ALL members of outer class
538+
System.out.println(outerField); // ✅ Works
539+
System.out.println(staticField); // ✅ Works
540+
}
541+
}
542+
543+
void localMethod() {
544+
final String localVar = "local";
545+
546+
// Local inner class
547+
class LocalInnerClass {
548+
void display() {
549+
System.out.println(outerField); // ✅ Outer class members
550+
System.out.println(localVar); // ✅ Final/effectively final locals
551+
}
552+
}
553+
554+
// Anonymous inner class
555+
Runnable r = new Runnable() {
556+
@Override
557+
public void run() {
558+
System.out.println(outerField); // ✅ Outer class members
559+
System.out.println(localVar); // ✅ Final/effectively final locals
560+
}
561+
};
562+
}
563+
}
564+
565+
// Interface with nested class
566+
interface MyInterface {
567+
// Class inside interface is IMPLICITLY static
568+
class NestedInInterface { // This is static by default!
569+
void method() {
570+
System.out.println("Inside interface nested class");
571+
}
572+
}
573+
}
574+
```
575+
576+
**Instantiation Examples:**
577+
578+
```java
579+
// Static nested class - no outer instance needed
580+
OuterClass.StaticNestedClass staticNested = new OuterClass.StaticNestedClass();
581+
582+
// Inner class - requires outer instance
583+
OuterClass outer = new OuterClass();
584+
OuterClass.MemberInnerClass inner = outer.new MemberInnerClass();
585+
586+
// Alternative syntax for inner class
587+
OuterClass.MemberInnerClass inner2 = new OuterClass().new MemberInnerClass();
588+
589+
// Interface nested class - implicitly static
590+
MyInterface.NestedInInterface interfaceNested = new MyInterface.NestedInInterface();
591+
```
592+
593+
**Access Rules Summary:**
594+
595+
```java
596+
public class AccessExample {
597+
private String instanceVar = "instance";
598+
private static String staticVar = "static";
599+
600+
static class StaticNested {
601+
void test() {
602+
System.out.println(staticVar); // ✅ Static members only
603+
// System.out.println(instanceVar); // ❌ No instance access
604+
}
605+
}
606+
607+
class Inner {
608+
void test() {
609+
System.out.println(staticVar); // ✅ Static members
610+
System.out.println(instanceVar); // ✅ Instance members too
611+
}
612+
}
613+
}
614+
```
615+
616+
**Common Pitfalls:**
617+
618+
```java
619+
// Pitfall 1: Confusing terminology
620+
class Outer {
621+
static class Nested { } // This is NOT an inner class!
622+
class Inner { } // This IS an inner class
623+
}
624+
625+
// Pitfall 2: Wrong instantiation syntax
626+
// Outer.Inner wrong = new Outer.Inner(); // ❌ Missing outer instance
627+
Outer.Inner correct = new Outer().new Inner(); // ✅ Correct
628+
629+
// Pitfall 3: Interface nested class confusion
630+
interface Test {
631+
class Helper { } // Implicitly static, not inner!
632+
}
633+
634+
// Pitfall 4: Static nested accessing instance members
635+
class Example {
636+
String field = "test";
637+
static class BadNested {
638+
void method() {
639+
// System.out.println(field); // ❌ Cannot access instance field
640+
}
641+
}
642+
}
643+
```
644+
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.
646+
647+
**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.

0 commit comments

Comments
 (0)