Re: problem with interface
Is there an error message? Please post it.
Otherwise please explain what is/is not happening
Please put your code in code tags to preserver the formatting. See the # icon above the text input box.
Re: problem with interface
Looks like you've got very confused...
To use the interface, you need to create an instance of a class that implements it (e.g. areAnagramsx) and then call a method on that object, for example:
Code :
AnagramFinder finder = new areAnagramsx(); // create object that implements the interface
boolean areAnagrams = finder.areAnagrams(s1, s2); // call method on object
The boolean result can be used directly, there's no need to compare it with a 'true' value because it already is a true or false value, e.g.:
Code :
boolean areAnagrams = finder.areAnagrams(s1, s2);
if (areAnagrams) {
// do stuff
}
Also, please use the Java conventions and start all class names with uppercase letters. Use meaningful names - if you can't think of a meaningful name for the implementation class of AnagramFinder, just call it AnagramFinderImpl.
Re: problem with interface
dlorde,
i wrote a full page reply, but my computer ate it. Would you be able to break down the setup for the interface?
// create object that implements the interface
AnagramFinder finder = new areAnagramsx();
AnagramFinder = create a new “type” AnagramFinder
finder = new object being created
New = new key word
areAnagramsx() = class that implements the interface
// call method on object
boolean areAnagrams = finder.areAnagrams(s1, s2
Boolean = keyword for the return value of the interface
areAnagrams = return value from the interface
finder.areAnagrams(s1, s2) = call the areAnagrams method with the finder
object
Is this about right?
Thanks, Buddy
Re: problem with interface
That's pretty much it, although to be more precise:
AnagramFinder finder = declare a reference variable called 'finder', of type AnagramFinder
finder = new areAnagramsx(); = create a new object of type areAnagramsx and assign it to finder (set finder to point to it)
In Java all variables for objects are references (generally known as 'pointers' in other languages). They must be declared to have a particular type, e.g. AnagramFinder, or areAnagramsx, or Object. This means they can reference ('refer to' or 'point to') any object of that type - including subclasses of that type - so an AnagramFinder variable can point to an areAnagramsx object (and an Object variable can point to objects of any class, because all classes are subclasses of Object). Bear in mind that the variable type determines what you can do with it; for example, an AnagramFinder variable can point to an areAnagramsx object, but you can only use it to access the AnagramFinder methods of the object. Similarly, an Object variable can point to an areAnagramsx object, but can only be used to access the Object methods it has.
If you don't assign an object to them, reference variables are null because they don't point to an object, and the only thing you can do with them when they are null is to check if they are null or point them to an object of a compatible type - anything else will cause a NullPointerException (one of the few examples in Java that explicitly describes reference variables as pointers!).
This means that you can only ever access objects (call methods on them, etc) indirectly, via these reference variables, and more than one variable can reference the same object. When you pass a variable to a method, the method receives a local copy of that variable that points to the same object.
boolean areAnagrams = finder.areAnagrams(s1, s2)
boolean = keyword declaring the type of variable areAnagrams (to match the type of the return value of the interface method).
Note that, for various reasons, Java has a few primitive types as well as object types. Primitive types aren't based on classes and methods, they're just 'built in' to the language. Their keywords start with a lowercase letter to make this clear: int, long, boolean, float, etc. However, because objects are often required in code, each primitive type has been given a corresponding wrapper class: Integer, Long, Boolean, Float, etc. Recent versions of Java provide an automatic mechanism called 'autoboxing', that converts between a primitive type and its wrapper class whenever necessary, so you can use them interchangeably without any hassle.