• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding classes inside interfaces

 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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........
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all friends.........
 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very interesting topic, You guys made my day.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got the answer at last....

thanks to all......
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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......
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic