inner class inside interface in Java

View previous topic View next topic Go down

Java inner class inside interface in Java

Post by BIT0112-Rokon on Sat Feb 12, 2011 4:35 am

Im not very sure why it is going on or why this is important or bla bla bla ....

But I found that it is possible to write an inner class inside an interface.

Let me show you the code,

Code:

public interface HelloWorld {
    public class Greetings {
        public void sayGreetings() {
            System.out.println("Hey, Good day!");
        }
    }
}

Code:

public class MainClass {
    public static void main(String[] args) {
        HelloWorld.Greetings greet = new HelloWorld.Greetings();
        greet.sayGreetings();
    }
}

Compile and run it... Its working nice.

Can anyone help me finding when it needs to use it or why?


Last edited by BIT0112-Rokon on Sat Feb 12, 2011 8:45 pm; edited 1 time in total

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: inner class inside interface in Java

Post by BIT0102-Mohaimin on Sat Feb 12, 2011 8:05 pm

When you told me this first in the morning, it didn't seem to be very amazing (Possibly I did not listen carefully). But now I must say, its amazing.

However, you have missed a more interesting thing happening here.
An inner class of an inner class cannot be accessed directly from outside. It can only be accessed by an object of the outer class. But its not true for an outer interface, you have shown it.
Try converting the HelloWorld to a class. You will get an error in the MainClass.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum