senior web programmer
senior web programmer
Java language: What is the point of interfaces?
Even if you can’t answer every part, any help would be greatly appreciated… I am a high school senior taking the AP Computer Science course…
In the Java programming language, what is an interface, and why would anyone use one? (It seems like an extra step to me…) How is it useful? What is the use of having pre-built interfaces in the Java Standard Class Library, such as Comparable and Complexity, if they are essentially empty shells that the programmer has to define every time s/he wants to use it? Am I mistaken?
Could you please explain the two interfaces Comparable and Complexity? What are some uses? Are there web-sites with clear examples?
How does the Java-reserved word “abstract” tie into this? What does it mean? How is it used? What is it for? Could you give me some examples?
Thanks for all the help…
No, Interfaces have nothing to do with multiple inheritance.
Java doesn’t have multiple inheritance, period. Because some idiot in marketing decided, that it would be “too complicated”.
Interface is a way do describe the object’s behaviour in terms of interacting with other objects, without actually implementing it.
take Comparable for example. It simply states, that a class, that implements comparable must have a function compareTo(), that takes another object as argument, and tells whether it is greater, less or equal to this object, according to some ordering.
It does not matter at this point what this ordering is, and what this function does to come up with the answer. In many cases just the knowledge that this function exists is sufficient for many useful applications.
Take void sort (Comparable []) for example. You can write a function that takes an array of objects, that implements Comparable, and sorts it in ascending (for example) order. To write such function, you don’t need to know what type the objects in the array are, what they do, or how they are used. All you need is a way to compare them, and that’s exactly what Comparable provides to you.
Oh, and about “abstract” key word. It is a way to label a function, that does not define implementation (or a class that contains abstract functions). It helps in some cases, when you want to implement some generic functionality to be customized in the superclasses. For example:
public abstract class Base
{
public abstract String getDescription ();
public String toString () {return “This is a ” + getDescription();}
}
public class A extends Base
{
public String getDescription () {”instance of A”;}
}
Note, how Base declares, that all it’s sublcasses must define a getDescription() function, and then actually uses it, even though it’s implementation is not defined.
Here again, we do not need to know how that function is implemented or what it will do – it’s enough to just require that it exists.
You are right, it is very related. You can think of interface as a class, that only has abstract functions.
Code Genome: Senior Web Developer