aspose file tools*
The moose likes Beginning Java and the fly likes What is an interface ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is an interface ?" Watch "What is an interface ?" New topic
Author

What is an interface ?

Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I have no problem spotting many issues ( in code ) that seem to be pretty advanced, but I cannot get the hang of this idea of an interface. I see syntax that looks alarming, ususally close to an entry into {} code block. This morning while doing a much anticipated re-code of earlier work, I believe I could use Interface java.lang.CharSequence much more effectively than the workarounds that I force-fitted into prototyping: I had resorted to using the disk-heads, Vectors, String(s) StringBuffers, char[]'s, TreeMaps and all manner of guesses when all I wanted to do was pass a mutable sequence of characters from one phase of the app to the next ( usually words, phrases, sentences or paragraphs - size may vary unexpectedly ); It does not even have to be mutable .... but it makes more sense to me to track where the changes are going on and just pass a section of memory ( that now has some processed characters ) down a control-flow path and implement strict copy semantics at each end of the control-flow path.

The idea or concept of an interface is intuitive, it is how to use Java Interfaces correctly that I am asking about.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74
I am not sure that I completely understand what you find difficult about interfaces. Basically an interface lets you shield one part of your code of the details of some other part of the code. This lets you separate concerns, so that your program is easier to understand once it gets larger.

CharSequence is a case in point. As you can see in the API docs a CharSequence is something that exposes its length, lets you retrieve a char at any given index and lets you get a sub-sequence which is also a CharSequence. You can also see that CharBuffer, String and StringBuffer are all CharSequences.

You could write a method that searches for a certain sub string for instance, and it would never have to know if it was searching a String, StringBuffer, CharBuffer or some other object that implements the CharSequence interface. That way, someone reading your search routine, knows that it only depends on the three things about the char sequence mentioned above. Also, if I had my own class SpecialString, I could write an adapter that implements CharSequence and then reuse your search routine passing in my own objects.

One thing CharSequence doesn't let you do is modify its contents. So if you need to have a MutibleCharSequence I guess you'll have to write it yourself.

OK, I hope this makes sense to you. If not, please, share some more details about what you are trying to achieve.


SCJP 1.4, SCWCD 1.4
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Well you certainly have given me enough to phrase my needs with narrower conciseness. The first use I had for anything like this, I managed to get a 'word count' type of implementation that would take a reader and come up with a list of words that were used in that document/file/source/feed - whatever one may wish to call it, the nature of which is displayable | printable characters. The immediate next evolution was a TreeMap in which the frequencies of occurence were noted for some paticular document / file / source / feed and with that accomplished I delved into
Threads and Regexs. Today I went back to re-code major portions and it occured to me that what I was passing around was a character sequence.

In general:


here, the vertical bars may need to be mutable, but I could do that by passing a new character sequence which is the result of some op, denoted here by a vertical bar and in my code by a Thread Object. I figured out how to implement Runnable, and often do that when coding test funcs, but the original concept - which holds valid at this point - is an operation that is best modeled in my mind by a Thread of Operation, which when complete may be thought of as applying Operation(i) to Cleartext(j) | and I may study combinatorics in the attempt to retire lattice plane Z(i,j) which may then become a point on a sombrero function

It originally occured to me to just use a char[], and consequent to your reply that may be the correct answer to my goal. I could easily with my skills attained thusfar write a class which implements CharSequence, along with some other stuff I need such as clear(), blank(), scramble() and other more sophisticated encipherments and rotators. It is ultra-easy and fast for me to write these things, it is the fundamental concept of how you can have something like set or something which only has func(); and have it operate, except by obsucurity. When I try to write an interface myself, it does not work. The best I can tell is that one declares an interface as a named something or other and there are no constructors nor anything else that 'works like it should' it just sort of ..... well, I don't know -> that's what the question is.

Set is becoming useful, along with some things such as this if I knew how to use them ~ but I always just do: Vector victor = new Vector(101,75); because then I can "see" where the Vector is keeping things. Let us say I had a String, or a Vector of words: My first attempt used File that was not of any use past the work of passing results of Operation(i,j) to Operation (i+1,j+1) and would like to be able to pass entire phrase collections, maybe even paragraphs == whatever -> along the chain of operations.

But let us say the class SpecialString has a method which gets the contents of the internal buffer, clearly checking bounds and failure points and pumping default values if needed to thwart failure such as null strings and so on. How would I pump these characters into a CharSequence, as in the return value from a func() such that this CharSequence is now a new whatchamacallit, somewhere else in ram other than where the stack frame for instance n+ij of SpecialString points to ?

This would simplifiy my design in that passing a *COPY* of SpecialStrings char[] at some points in the control flow may be of use later and thus would retire trying to code in workarounds for boundary conditions that I cannot at this point guess at.

StringBuffer does something not unlike this in it's toSring()

IOW - how to write:

[ November 06, 2007: Message edited by: Nicholas Jordan ]
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74
Honestly, you've lost me here. What kind of application are you writing? Has it to do with signal processing or is it a kind of text processor?

About the code sample at the end.


This will do exactly the same as your sample, and it is shorter. You don't need to do s.toString() (it just returns s itself), you don't need to copy the string into a StringBuffer and you don't need to get a String back from the StringBuffer. Since String implements CharSequence you can use it anywhere a CharSequence is expected. So, as a matter of fact, you don't need SampleSimple, as it has only one operation, namely to get the original String as a CharSequence, but you can use the original string as a CharSequence already.

You have to understand that returning something as "CharSequence" or some other interface it implements, does not change the object you return. It just makes sure that the calling function only uses the methods declared in that interface. For instance, String has an endsWith() function, but the caller of getContents() will not be able to use it. The compilation will fail if you try.



cs points to exactly the same object as s, but it only uses part of the functions on s, namely, the ones declared in CharSequence.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I can see how my phrasing of the matter lacks conciseness, two points emerge here 1. it looks like the fundamental question is largely ansered by you can use the original string as a CharSequence already. but there are additional clarifications I need. 2. Honestly, you've lost me here. What kind of application are you writing? Has it to do with signal processing or is it a kind of text processor? Suffice it to say the application of signals processing to textual signals has not been adequately nomenclatured in the published literature. I may become unclear by trying to express in reasonable terminology applications of CholeskyDecomposition to a textual stream, in part because I have not worked out sufficient mental grounding to provide reasonably worded fulfillments of your wonderment. The work is exploratory, and intends to apply artificial intelligence to operator intervention in the selection of filtering bias for further refinement by operator evaluation.

PSO and other techniques appear as appealing avenues of exploration, but first I have to determine an efficient, or at least accepted, method of passing around character sequences.

Given SampleSimple and CharSequence returned by getContents()

Are the other methods of CharSequence available to be called on cs ? This is critical to my question and may or may not provide further formulations: Given the second code snippet, and the varaiable cs, what methods may be called on cs ?

IOW: are

Available on the variable cs in the second code snippet:CharSequence cs = ss.getContents(); are these methods callable on the variable cs ? Further, other than concurrency issues ( which are extremely perceptable for me for some reason ) is CharSequence the most processor efficient approach for massive data feeds on performance critical paths in processor intensive code sectious such as would show up on profiling with known and standard approaches to profiling ? Note that I have made ample provision for parallelizing compilers such as avoiding multiple thread accesses on data structures that are built up in the progress of the program.

Say for example:


What I have tried to examine here is also addressed by escape analysis, I have made more than ample provisions for avoiding this situation in my first draft coding, but will soon become so deeply entrenched in way-on-out-there computer science that I may forget to deal with this and want to establish java best practices on this before getting coding habits so deeply ingrained in my fingers that I trash some buffer somewhere at a product rollout on a network feed.

Some of my original quesions are answered, but an interface is neither an object nor a primitive, so I need to be effective in my contemplation of what happens to the ss object - is it even an object and what is seq vis-a-vis ss ? In other words, what exactly is the compilation effect of the copy operator here.
[ November 11, 2007: Message edited by: Nicholas Jordan ]
Jan van Mansum
Ranch Hand

Joined: Oct 19, 2007
Posts: 74
"Are the other methods of CharSequence available to be called on cs ?"

All the methods declared in CharSequence can be called on cs, because you declared cs as a CharSequence.

"... is CharSequence the most processor efficient approach for massive data feeds on performance critical paths in processor intensive code sectious such as would show up on profiling with known and standard approaches to profiling ?"

CharSequence is only an interface. How efficient your algorithm will be, depends on the implementation details of your algorithm, not on CharSequence. It may be that you need to access the data encapsulated by CharSequence in ways that this interface will not allow. In that case you need to use another interface, or work with the underlying class directly, all depending on the details of what you are trying to achieve.

".. I need to be effective in my contemplation of what happens to the ss object - is it even an object and what is seq vis-a-vis ss"

ss is an object and so is seq. seq is a field on ss.
[ November 13, 2007: Message edited by: Jan van Mansum ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
I note in your signature that you Sun Certified Programmer for Java 2 Platform: In the interest of completeness and in that the orginal title of this Thread is what is an interface, I provide the following work from a few hours ago: (I was trying to do exactly what we are talking about here)



[J vM:]   seq is a field on ss.
Can you elaborate on the technical matters of this ? I believe your choice of words here answers my post title question exactly and ( I anticipate ) provides a clear comprehension. Proposed alternate wording:

An interface on an Object behaves as though it were a variable contained by that object. Whether the field is mutable or not depends on the class definition and the nature of the field, not on details of compiler implementation or compile time switches.

[J vM:]   How efficient your algorithm will be, depends on the implementation details of your algorithm, not on CharSequence.
In other words, there is not a great deal of performance-critical issues here, so accessing Pattern.compile("\\b\\w+\\b); vis-a-vis char[233] vis-a-vis String.charAt(322) vis-a-vis Vector.get(i-th position) is not something I should hold up prototyping over trying to eek out little mouse-squeaks of performance gain trying to do Research grade work in Parallel Computational Optomization like I was Jet Propulsion Laboratory - the mental territory provided by an interface would be a better selection basis: Whether it suits my needs at some point in the code path.

Consider the following snippet:



This snippet was written just now while considering your reply. I did not attempt a compile so as to avoid becoming focused on other issues not within scope of thread title. Note in paticular ;// now what ?

The interface idea is appealing, and the progress of this discussion suggests it would be effective for my design paradigm, but narrowly: Is there any way another portion of the program could modifiy internalBuffer (if I restric access narrowly to the methodology specified in the formal definition of);public interface CharSequence

[ November 13, 2007: Message edited by: Nicholas Jordan ]
[ November 13, 2007: Message edited by: Nicholas Jordan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is an interface ?