Monday, April 16, 2012

Public, Protected, and Private Interfaces... Yes Interfaces.

One of the junior developers came to me on Friday and asked if an interface could be private, or protected. I said sure, but it must be an inner interface. The interface must be contained within a containing class. So I decided to create a couple of examples of interface fun for you to appreciate.

The NetBeans project is located here: interfaces.zip

code

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
26
27
28
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.bluelotussoftware.example.core;
 
/*
 * Odd looking import to allow C to be used without nesting (chaining).
 */
import com.bluelotussoftware.example.core.A.B.C;
 
/**
 *
 * @author John Yeary
 * @version 1.0
 */
public class A {
 
    /**
     * This can only be used by classes, or interfaces declared inside the
     * Parent class.
     */
    private interface W {
 
        void w();
    }
 
    /**
     * This can be used by classes, or interfaces declared inside the parent
     * class, or the same package.
     */
    protected interface X {
 
        String x();
    }
 
    /**
     * This can be used by classes, or interfaces declared inside the parent
     * class, or classes inside the same package.
     */
    interface Y {
 
        String y();
    }
 
    /**
     * This can be used by any class.
     */
    public interface Z {
 
        String z();
    }
 
    /**
     * Inner class
     */
    class B implements W {
 
        @Override
        public void w() {
        }
 
        class C extends B {
 
            @Override
            public void w() {
                System.out.println("NO!");
            }
        }
    }
 
    private void run(Y z) {
        System.out.println(z.y());
    }
 
    void execute() {
        // Anonymous method local inner class.
        run(new Y() {
 
            @Override
            public String y() {
                return "YES!";
            }
        });
 
 
        // Anonymous method local inner class with method call.
        System.out.println(new X() {
 
            @Override
            public String x() {
                return "It is a question of something...";
            }
        }.x());
 
 
        // Construction of a multi-nested inner class
        C c = new A().new B().new C();
        c.w();
    }
 
    public static void main(String... args) {
        new A().execute();
        // Constructor chaining with a method call.
        new A().new B().new C().w();
    }
}

3 comments :

Unknown said...

I hope this young programmer will not put all them code in one file after this advice :)

John Yeary said...

If he put them in his code... I would laugh... and then kill him for doing it.

Here are my thoughts on it. Just because you can create this strange assortment of interfaces, and inner classes does not mean you should.

There are cases where you might use an interface that is private, and the inner classes which are in the example are often seen as listeners.

However, if I came across this code in production I would fix it, or make the person who did it fix it.

I do a Java Certification Boot Camp where I have code like this though. It is instructional. ;-)

Unknown said...

Interesting.............

Popular Posts