Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 12-29-2010, 01:56 PM   #1
Beta Member
Join Date: Dec 2010
Posts: 4
Default What is Interface ?

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.56 is offline   Reply With Quote
Old 12-29-2010, 01:57 PM   #2
Beta Member
Join Date: Dec 2010
Posts: 4
Default Re: What is Interface ?

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();
for(int i = 0; i < ArrayofShapes.size(); i++)

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

peter.15 is offline   Reply With Quote
Old 12-29-2010, 02:12 PM   #3
Beta Member
Join Date: Dec 2010
Posts: 2
Default Re: What is Interface ?

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-
mike.67 is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 03:37 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0