Skip to content

Commit b010391

Browse files
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

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed
Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
1-
/*
2-
https://www.geeksforgeeks.org/types-of-interfaces-in-java/?ref=asr9 IMP Link Open and read it.
3-
4-
https://www.geeksforgeeks.org/interface-nested-class-another-interface/?ref=ml_lbp
5-
6-
https://www.geeksforgeeks.org/match-lambdas-to-interfaces-in-java/?ref=asr8
7-
8-
We can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interfaces declared outside any class can have only public and default (package-private) access specifiers. In Java, nested interfaces (interfaces declared inside a class or another interface) can be declared with the public, protected, package-private (default), or private access specifiers.
9-
10-
A nested interface can be declared public, protected, package-private (default), or private.
11-
A top-level interface (not nested) can only be declared as public or package-private (default). It cannot be declared as protected or private.
12-
13-
Uses of Nested Interfaces
14-
In Java, nested interfaces can be used for a variety of purposes, including:
15-
16-
To group related interfaces together: By nesting one interface within another, you can organize related interfaces in a more logical and readable way. This can make your code easier to understand and maintain.
17-
To create more secure code: By making an interface nested inside a class, you can limit its scope and prevent it from being accessed outside of that class. This can make your code more secure and less prone to errors.
18-
To implement multiple interfaces: By nesting interfaces, you can implement multiple interfaces in a single class, without cluttering up the global namespace with too many interface names.
19-
To create callbacks: Nested interfaces can be used to create callback functions, where an object can be passed to another object and that object can call back a method defined in the nested interface.
20-
To define a contract between classes: By using nested interfaces, you can define a contract between classes, where each class implements the same interface, but provides its own implementation. This can make your code more modular and easier to test.
21-
22-
working of interface inside a class
23-
*/
24-
251
import java.util.*;
2+
263
class Parent {
274

285
// Nested Interface
@@ -31,18 +8,14 @@ interface Test {
318
}
329
}
3310

34-
// Child Class
3511
class Child implements Parent.Test {
36-
public void show()
37-
{
12+
public void show() {
3813
System.out.println("show method of interface");
3914
}
4015
}
4116

42-
class NestedInterface
43-
{
44-
public static void main(String[] args)
45-
{
17+
class NestedInterface {
18+
public static void main(String[] args) {
4619
// instance of Parent class
4720
// with Nested Interface
4821
Parent.Test obj;
@@ -53,4 +26,49 @@ public static void main(String[] args)
5326
obj = t;
5427
obj.show();
5528
}
56-
}
29+
}
30+
31+
/*
32+
https://www.geeksforgeeks.org/types-of-interfaces-in-java/?ref=asr9
33+
34+
https://www.geeksforgeeks.org/interface-nested-class-another-interface/?ref=ml_lbp
35+
36+
https://www.geeksforgeeks.org/match-lambdas-to-interfaces-in-java/?ref=asr8
37+
38+
We can declare interfaces as members of a class or another interface. Such an interface is called a member interface or
39+
nested interface.
40+
41+
Interfaces declared outside any class can have only public and default (package-private) access specifiers.
42+
43+
In Java, nested interfaces (interfaces declared inside a class or another interface) can be declared with the public,
44+
protected, package-private (default), or private access specifiers.
45+
46+
A nested interface can be declared public, protected, package-private (default), or private.
47+
A top-level interface (not nested) can only be declared as public or package-private (default).
48+
49+
It cannot be declared as protected or private.
50+
51+
Uses of Nested Interfaces:
52+
In Java, nested interfaces can be used for a variety of purposes, including:
53+
54+
To group related interfaces together: By nesting one interface within another, you can organize related interfaces in a
55+
more logical and readable way. This can make your code easier to understand and maintain.
56+
57+
To create more secure code: By making an interface nested inside a class, you can limit its scope and prevent it from
58+
being accessed outside of that class.
59+
60+
This can make your code more secure and less prone to errors.
61+
62+
To implement multiple interfaces: By nesting interfaces, you can implement multiple interfaces in a single class,
63+
without cluttering up the global namespace with too many interface names.
64+
65+
To create callbacks: Nested interfaces can be used to create callback functions, where an object can be passed to another
66+
object and that object can call back a method defined in the nested interface.
67+
68+
To define a contract between classes: By using nested interfaces, you can define a contract between classes,
69+
where each class implements the same interface, but provides its own implementation.
70+
71+
This can make your code more modular and easier to test.
72+
73+
working of interface inside a class
74+
*/

0 commit comments

Comments
 (0)