PDA

View Full Version : [Highschool - computers] I don't understand the purpose of interfaces. (In java)


dooey100
January 20th, 2008, 04:45 PM
Hi, I'm taking a course in java programming and I just learned how to use interfaces. I know how to use them properly, but I don't see the point. You still have to write the methods for each class that uses the interface, and the only reason I see to use an interface would be to make sure you don't forget any methods.

I know that you can use interfaces for constants, but unless you have a lot of constants, if wouldn't be much trouble to put the constants with all the other variables.

I haven't done any big projects with many classes yet, so they may be more useful in practice then it looks like.

On a slightly unrelated note, didn't there use to be a board for programming?

tsugomaru
January 20th, 2008, 05:13 PM
We used to have a programming board but it was shut down due to inactivity. I remembered at one point when there was less than one thread created per month. If you have a question like this, your answer would be better answered on a Java programming forum because more people know what they are doing there or the answers may be found there.

~Tsugomaru

sleeplessdragn
January 20th, 2008, 06:10 PM
It is often nice, when writing code that other people will be using, to force them into a path, so to say. Interfaces let you keep your code scheme very apparent, as well keeps things clean and easy when dealing with projects that involve heavy inheritance.

dooey100
January 20th, 2008, 08:21 PM
Ah, that makes sense. I never thought about team/group projects.

skishmonkey72
January 21st, 2008, 12:40 AM
Like sleeplessdragn said, it's really helpful in team projects to have specific classes in a project to implement a particular interface, so one can expect certain things from an implementing class. Another thing is for when you want to have classes inherit certain things. It doesn't always make sense to have a class to inherit everything from a superclass. For example, Arabian can extend Horse, and inherit methods and variables specific to any Horse, but it could also implement Rideable, and inherit specific methods and constants required for a Rideable object. (Rideable objects are not necessarily Horse objects, but could also be something like Camel). But you can't necessarily put those methods and constants in the Horse superclass, because not all horses are intended to be ridden. In short, interfaces are a useful way to circumvent or model multiple inheritance.