Commit b010391
committed
feat: Demonstrate nested interface implementation with Parent.Test and Child
WHAT the code does:
- Defines a class Parent containing a **nested interface** Test with a method show().
- Defines class Child that implements Parent.Test and provides a concrete implementation of show().
- In NestedInterface.main():
- Declares a reference of type Parent.Test.
- Creates an instance of Child and assigns it to the interface reference.
- Calls show() on the reference, which executes Child’s implementation and prints "show method of interface".
WHY this matters:
- Demonstrates **nested interfaces**, where an interface is defined inside a class or interface.
- Nested interfaces provide scoping: Test is tied to Parent, so its full name is Parent.Test.
- Shows how nested interfaces can be used to group related behavior with their enclosing class, improving modularity.
- Illustrates polymorphism: Parent.Test reference holds a Child object, invoking overridden behavior at runtime.
HOW it works:
1. Parent defines Test interface inside its body.
2. Child implements Parent.Test, fulfilling the contract.
3. In main, Parent.Test obj acts as a reference to Child.
4. Calling obj.show() triggers Child’s show() method, demonstrating runtime polymorphism.
Tips and gotchas:
- Nested interfaces are implicitly static — they don’t require an instance of the enclosing class.
- Common in frameworks like Android: e.g., `View.OnClickListener` is a nested interface inside `View`.
- Use nested interfaces when the contract is strongly associated with the enclosing class, avoiding namespace pollution.
- Don’t overuse nested interfaces; if the interface is broadly applicable, define it at the top level instead.
Use-cases / analogies:
- Event listeners: Button.OnClickListener, where the listener interface is nested inside the Button class.
- Data structures: Map.Entry is a nested interface inside Map.
- Real-world analogy: A company (Parent) has job roles (nested interfaces) that employees (Child) must implement.
Short key: java-nested-interface parent-child polymorphism scoping modularity.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 28482ff commit b010391
1 file changed
+50
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
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 | 1 | | |
| 2 | + | |
26 | 3 | | |
27 | 4 | | |
28 | 5 | | |
| |||
31 | 8 | | |
32 | 9 | | |
33 | 10 | | |
34 | | - | |
35 | 11 | | |
36 | | - | |
37 | | - | |
| 12 | + | |
38 | 13 | | |
39 | 14 | | |
40 | 15 | | |
41 | 16 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 17 | + | |
| 18 | + | |
46 | 19 | | |
47 | 20 | | |
48 | 21 | | |
| |||
53 | 26 | | |
54 | 27 | | |
55 | 28 | | |
56 | | - | |
| 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 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
0 commit comments