Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes regarding classes inside interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "regarding classes inside interfaces" Watch "regarding classes inside interfaces" New topic
Author

regarding classes inside interfaces

karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

i just tried to declare a class inside an interface and surprisingly it worked.........

i was able to declare a class with abstract modifier and invoke its constructor directly from another class inside the same file using proper convention.........
i am so much confused and i couldn't match it with any of the fundamental rules in java......

can anyone tell me the legal modifiers that can be used for a class within an interface.....

do they have any implicit modifiers just like methods and variables........


KARTHICK.C , SCJP6-93%
(Born to Win)
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Could you show us the code?



That could have several interpretations. Let's make sure we're all talking about the same thing.

-Cameron McKenzie
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Hello Karthick, this particular question is very interesting. As you know interface fields are constants so they cannot be changed. Well defining a class inside a interface will help you define fields and then access them and set them using the class apis. Also If you define a class within an interface its public and static by default. So this makes it nested class in a interface and can be instantiated using the interface name.

This is what i tried...
Interface class. InterfaceClass.java


AccessInterface.java


You can even set the x using setters. well the implicit modifiers are public and static. Well the interface class is not final as i tried to extend it and it din't give an error.

Well i hope this is what your question was on....

[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196



Surprisingly this compiles and the output is
method

How abstract and static modifiers live together.......

If i simply create an object of class A like this

i get the compiler error that method I.A is abstract and can't be instantiated.......
(I have implemented it using anonymous class here... so that's not my question )

how static modifier and abstract modifier live together.......
so i suppose that static modifier have different meaning when prefixed to classes....
am i right?

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

If you say like that then even nesting interfaces inside interfaces is also possible and then if classese in a interface are considered as public and static implicitly then the nested interface(100% abstract class) will be public and static. And surprisingly nested interfaces also compile properly. Then you can obviously use the anonymous class to implement the inner interface. These things are possible but i think its bad programming style. Interfaces should have a single specific role and having more roles in it may hamper the cohesion.

I.A ob=new I.A();

i get the compiler error that method I.A is abstract and can't be instantiated.......


You may be knowing that we cannot instantiate a Abstract class be it inner or a top level class. About anonymous classes, we can even have anonymous implementers also which can be used without actually implementing the interface.
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Nitish Bangera wrote:

I.A ob=new I.A();
i get the compiler error that method I.A is abstract and can't be instantiated.......
You may be knowing that we cannot instantiate a Abstract class be it inner or a top level class. About anonymous classes, we can even have anonymous implementers also which can be used without actually implementing the interface.


ya i know that...
but in my code i have instantiated a local class that extends that abstract class.....
i haven't instantiated an abstract class which is obviously not possible......

HOW STATIC MODIFIER AND ABSTRACT MODIFIER LIVE TOGETHER

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Actually abstract and static don't go together but now that you have tried it means for nested classes it can be true just like for inner classes abstract can go with private.
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
Just to cite a real-world example, it looks like Map.Entry is a nested class of the interface
Map: Map_interface_doc
although the documentation elswhere refers to it as a sub-interface.


SCJP 88% | SCWCD 84%
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

thanks to all friends.........
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Very interesting topic, You guys made my day.


Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

i got the answer at last....

thanks to all......
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Care to share the answer. I think apart interface not having static methods abstract can go with any thing but final for inner classes.
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Nitish Bangera wrote:Care to share the answer. I think apart interface not having static methods abstract can go with any thing but final for inner classes.



the classes inside an interface are implicitly static.......
the abstract modifier is a marker that the class is not fully implemented and that we need to implement it fully before using it......

so we can have classes that are both STATIC and ABSTRACT
just like top level abstract classes nested inside another class......
this compiles fine.....
and hence

also compiles fine.....

note that here both classes A and B are static....
A-implicit
B-explicitly marked.....

sorry that i didn't share this before....
i was in a hurry that day.....
sorry my friend......
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding classes inside interfaces
 
Similar Threads
interface
can we use non access modifiers combinely for members
Modifiers for anonymous inner classes and static nested classes
member interface
why we cann't make a class private and protected