What is Interface ?

steve.56

Beta member
Messages
4
Hi I have read through some notes on interface and would like to clarify a few things.

objecs define their interaction with the real world via methods . methods form the objects interface. interface is a group of method with empty bodies. what is the benefit of using interface

when writing a program to use this
1. The main program has the word main and defines the objects used.
2. the next java file has the syntax interface name (eg interface flower) and defines all the methods
3 do I need to create a third java file to define what each method does ? is this how it works .
 
steve, you are thinking in non-coding terms.

Sofia, to answer your question, I am going to try to attempt a metaphor for you that will help you understand. To me, an interface is more like being in the Boy Scouts. Interfaces are like badges, they say what you are able to do. For example, if you get your...Fire badge, everyone who sees this badge knows that you can create a fire. More than one boy scout gets this badge and will make a fire differently, but nonetheless, they all have the badge that says they can make a fire.

So if we apply this to interfaces, it helps in many cases. Some default interfaces, one that comes to mind is Comparable, makes it so that the object you gave the interface to has the ability to DO comparable things. When you implement interfaces into an object you HAVE TO put in all the methods in the interface, or else it will not work. This way the program knows for a fact that you have that badge.

This helps in many things. Let's say we have a Shape Interface with a draw method. If we were to create an array of interfaces and have objects like Triangle, Square, Polygon, etc. You can do many things. Here is some pseudo-code I can think of.

Triangle Shape1 = new Triangle();
Square Shape2 = new Square();

ArrayList<Shape> ArrayofShapes = new ArrayList();
ArrayofShapes.add(Shape1);
ArrayofShapes.add(Shape2);
for(int i = 0; i < ArrayofShapes.size(); i++)
ArrayofShapes.get(i).draw;

---------------------------------------------------
By creating an interface and then implementing said interface, you can abstract what things can do, especially if you add them to an array. The draw method of both the Triangle and the Square will draw DIFFERENT things but the array does not care. It knows that there is a draw method and will activate it. I think that is it for an interface, it just makes it able to DO something..
----------------------------------------------------
So to kind of answer your questions:
~ 1 file will have the interface methods that are empty.
~1 or more files will have objects that implement this interface.
~1 file will have the main execution method that creates these objects and does all the things you need it to do.

An interface is useful when you have a set of methods that need to be defined differently for different classes. You could always define the methods inside the class declarations as well. When I need to use the same group of methods I just reference to the interface and define the functions for that particular application
 
I'm looking for a way to create a small, tasteful popup notification (that just contains some text or something) that shows up on the taskbar and fades away shortly thereafter (or just disappears would be fine) in response to some event. I wondered if anyone has done that sort of thing before, and/or knows of class(es) I could use to accomplish this sort of thing.
__________________
"Beware of bugs in the above code; I have only proved it correct, not tried it."
-Don Knuth-
 
Back
Top Bottom