So I've been searching all over the web what interfaces, and what it comes down to is:
An interface just says what an object can do, but not how to do it.
Why would this make me use an interface? When I have to type the method name anyway?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
So I've been searching all over the web what interfaces, and what it comes down to is:
An interface just says what an object can do, but not how to do it.
Why would this make me use an interface? When I have to type the method name anyway?
IMO interfaces are one of the most powerful (and beautiful) features of java, but also can be the hardest to wrap one's head around. It allows a programmer many things from multiple inheritance to abstraction/encapsulate of a behavior. In other words, they provide a great many things that facilitate writing reusable and easily maintainable code.
If you are familiar with java swing, the *Listener interfaces are a perfect example. All the caller (the swing/awt API) knows is that listeners are registered and are called when appropriate - this allows the swing API to be independent of any solid implementation: anything can be a listener (so long as it implements the appropriate interface). A custom example of this can be found in the following article: How To Use the Observer Pattern
Lastly, google "java design patterns". They are techniques that are proven to work to write re-usable and easily maintained code, and rely heavily the use of abstraction through interfaces and abstract classes.
Edit: Cross posted at Can't understand.... While not against the rules, we ask posters be forthright about it. For reasons why, see The Problems with Crossposting
Last edited by copeg; February 13th, 2011 at 12:24 PM.
Try designing a plugin acceptable program without them.
One more question, are there certain things you can not do unless you implement an interface?
What doesn't make sense? If it were based upon classes: one can only use instances of that class. Based upon interfaces: ANY object can be a listener. Like I said its not an easy concept to wrap one's head around, and usually understanding comes more with experience than anything else.
No...that function defines the interface, but you can't add the method, you must add a class that implements ActionListener - you must tell the compiler there is an ActionListener throughimplements or new ActionListener (for an anonymous class). From the perspective of the class receiving the ActionListener object, all it cares about is the actionPerformed method.I could just type...right?