• Post Reply Bookmark Topic Watch Topic
  • New Topic

An interface within a class  RSS feed

 
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guyz,

Good Day..

I am debugging a code written by someone (luckily that someone was gone already...) and found this in code in the class he has written.



I am quite confused whta does this code buy you? Having an interface within a class definition.
Do someone know some practical application for this?

Thanks.
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whether it's useful depends on how it is used So ... how is it used by the remainder of the code?
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mark reyes:

I am quite confused whta does this code buy you? Having an interface within a class definition.
Do someone know some practical application for this?


All this buys you is that it communicates that the interface is closely related to the class. In fact, the java compiler will translate this to a top level interface.

More generally, nested interfaces can also be protected and private, in this way giving you more options for access control.

And even more generally, instances of (non-static) inner classes are tightly bound to an instance of the outer class, being able to access members of the outer instance. This doesn't apply to interfaces, though.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A public interface inside a class seems fairly pointless to me. I'd prefer to see it in its own separate file.

However, as already mentioned, only an interface inside a class can be declared private or protected. Sometimes, that makes sense, when the interface is only appropriate for use inside the outer class (or its subclasses).
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you never really need an interface declared inside a class, they can be useful sometimes. Typically they make sense if the interface is something only used in conjunction with that class. Defining one inside the other can communicate that the inside definition is useful in support of the outside entity. The best example I know offhand is Map.Entry, which is actually an interface inside an interface. The Entry interface is really of interest only as part of a Map. They could have defined it separately, but they chose to define Entry as part of a Map to better communicate the relationship.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr. Ulf,

Good Day...

how is it used by the remainder of the code?


I found this line from the project code.



From my own understanding and the reply of the other poster, the subclass DicerEquip created a class that implements the interface contract and implemented the contract depending on the needs of the subclass...

I think as pointed out earlier post, the interface should be place on a separate file instead.

But I just want to know, when will be the practical time that I will think to have an interface embedded in a class when I am designing my classes hierarchy? Or is it really practical to have one in real life software application?

Pardon me if my question maybe vague, because as of my current understanding of the java language I would have not thought about this as of the moment.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Mark]: But I just want to know, when will be the practical time that I will think to have an interface embedded in a class when I am designing my classes hierarchy?

I think Ilja, Peter and I were all addressing this. Did any of those answers make sense to you? It's not very helpful to simply repeat your question without providing some feedback on the answers given.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr. Jim,

Good Day..

I really appreciate all the answers that you had given to me and take note all of it really.

Perhaps my understanding of the language is really not that deep though since I just started to learn the language and OO principle late last year. But I did try to comprehend it as much as I can.
Based on the program that I have read, I made conclusion that a public interface within a class should have been declared on its own file since the subclass just really implements the same contract nonetheless. I would'nt have arrived in that conclusion if you have'nt pointed me in that direction.
I just hope my conclusion was right.

I think I need to study more on how to design class hierarchy and exposed myself on cases such as this. Before I was just using nested inner classes and got hold of nested inner interface just these days. I should'nt stop learning perhaps and there's more to learn.

Thanks to all your valuable suggestion.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!