Where it is useful to have nested classes in an interface?

Abhishek Saxena

In which scenario can an interface have nested classes?

The following code is allowed and valid.

public interface Iface {

    void show();

    class ifaceClass {
        int x;

        public ifaceClass() {
            System.out.println(x);
        }
    }
}

I am also struggling to make object of class ifaceClass.

EDIT :

I am able to make object like this

public class Test implements Iface {    

    public static void main(String[] args){
        ifaceClass  ifaceClassObj = new ifaceClass();
    }

    public void show() {    

    }
}

I noticed if Test has not implemented the Iface then I needed following import,

import com.jls.Iface.ifaceClass;

But it boiled down to same problem that why not use it as a just another class.

What the difference or value addition with this approach ?

CoderCroc

Where it is useful to have nested classes in an interface?

There is no such case which can only be fulfilled with inner class of interface. It is syntactically valid to have inner class in interface and for the class which implement interface can create instance of class and apart from that Interface.Class can also make that class accessible because it can not be private at all.

I noticed if Test has not implemented the Iface then I needed following import import com.jls.Iface.ifaceClass;

Not necessarily, if your interface is accessible your inner class will automatically become accessible.Here you are trying to access class directly without even importing interface in that case following statement need above import statement.

ifaceClass  ifaceClassObj = new ifaceClass();

But it boiled down to same problem that why not use it as a just another class. What the difference or value addition with this approach

Exactly, creating another class can also provide you the same facility and I have never seen any use case in my day to day programming which can only be fulfilled with inner class of interface.It does not provide anything else than accessibility through the interface.

I have used it once which I think quite a bad practice though. One day we need to implement one common method in different classes which are implementing interface say X and we wanted to add one extra method to be used by all this classes to add one kind of check on the Object which only check some parameter and return boolean even though that use case can be fulfilled in other way but to be specific that it is only intended for classes which are implementing this interface we have added class in interface so that we can provide that method to implementing classes.(NOTE : Nowadays default method can be used in this case instead of inner class)

Here, it is wise to note that in huge projects it is quite impossible for anyone ( other than creator ) to note that any interface has inner class. So, until we implement that class or manually check the interface we can not came to know that interface has inner class.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Classes nested into an interface

From Dev

Classes that implement an interface have more methods

From Dev

Classes that implement an interface have more methods

From Dev

Do we have to create interface for Utility classes?

From Dev

Nested classes don't have a namespace

From Dev

Are nested Using statements useful?

From Dev

Where is Date roll is useful?

From Dev

Where is Date roll is useful?

From Dev

Windsor castle injecting dependency in nested classes with root interface resolved at runtime

From Dev

How to have an abstract class require an interface to be implemented by descendant classes?

From Dev

all classes which have a certain annotation and implement a certain interface

From Dev

Interface. Why is useful in this case

From Dev

webapi entity frameworks - best way to return nested classes where

From Dev

Where should implement the methods of AsyncTask class if have multiple classes in Android?

From Dev

Where and why is identity function useful?

From Dev

Where is struct with no named members useful?

From Dev

Github Atom Interface - How do I know what classes elements have in the interface

From Dev

Github Atom Interface - How do I know what classes elements have in the interface

From Dev

Nested Interface

From Dev

Nested Nested Comparator Classes

From Dev

Android app UML classes diagram it is useful?

From Dev

When is it useful to have broadcast data in deserialized form?

From Dev

Is it ever useful to have reference type declared as const?

From Dev

Why does PHP have abstract classes if you can use an interface and traits?

From Dev

In Java, is there some kind of Interface-like that enforces its implementing classes to have specific fields

From Dev

Why does PHP have abstract classes if you can use an interface and traits?

From Dev

What is the proper design pattern to use when you have a bunch of extremely similar classes that can't share an interface?

From Dev

Where is strassen's matrix multiplication useful?

From Dev

C++ allow derived classes of friend to have access to private nested class

Related Related

  1. 1

    Classes nested into an interface

  2. 2

    Classes that implement an interface have more methods

  3. 3

    Classes that implement an interface have more methods

  4. 4

    Do we have to create interface for Utility classes?

  5. 5

    Nested classes don't have a namespace

  6. 6

    Are nested Using statements useful?

  7. 7

    Where is Date roll is useful?

  8. 8

    Where is Date roll is useful?

  9. 9

    Windsor castle injecting dependency in nested classes with root interface resolved at runtime

  10. 10

    How to have an abstract class require an interface to be implemented by descendant classes?

  11. 11

    all classes which have a certain annotation and implement a certain interface

  12. 12

    Interface. Why is useful in this case

  13. 13

    webapi entity frameworks - best way to return nested classes where

  14. 14

    Where should implement the methods of AsyncTask class if have multiple classes in Android?

  15. 15

    Where and why is identity function useful?

  16. 16

    Where is struct with no named members useful?

  17. 17

    Github Atom Interface - How do I know what classes elements have in the interface

  18. 18

    Github Atom Interface - How do I know what classes elements have in the interface

  19. 19

    Nested Interface

  20. 20

    Nested Nested Comparator Classes

  21. 21

    Android app UML classes diagram it is useful?

  22. 22

    When is it useful to have broadcast data in deserialized form?

  23. 23

    Is it ever useful to have reference type declared as const?

  24. 24

    Why does PHP have abstract classes if you can use an interface and traits?

  25. 25

    In Java, is there some kind of Interface-like that enforces its implementing classes to have specific fields

  26. 26

    Why does PHP have abstract classes if you can use an interface and traits?

  27. 27

    What is the proper design pattern to use when you have a bunch of extremely similar classes that can't share an interface?

  28. 28

    Where is strassen's matrix multiplication useful?

  29. 29

    C++ allow derived classes of friend to have access to private nested class

HotTag

Archive