COME ON I HEARD "YOU CAN SPECIFY WHAT CLASS SHOULD DO,NOT HOW".BUT WITHOUT INTERFACE ,EXTREME JAVA PROJECTS CAN BE MADE!! WHY TO MAKE COMPLICATION USING INTERFACE?

Recommended Answers

All 3 Replies

An interface defines a set of methods that must be implemented by any class that implements the interface. Eg You could define an interface with a calculateArea method. Then define all kinds of shape classes (Square, Circle etc) that implement the interface. Those classes may have a common Shape superclass, but you could also define "Building Plot or "PlayingField" classes that implement calculateArea but are not subclasses of Shape. Now you can call calculateArea for any instance of any of those classes without needing to know anything else about them. It's a way around not having multiple inheritance.

Here is an excellent tutorial on the subject

In Java 8 interfaces with just one method ("functional interfaces") are an essential part of how lambdas have been added to Java without breaking anything.

interfaces aren't used to make complications. actually, they make working on your project a lot easier.
if you don't understand why developers write to interfaces rather then to implementations, you haven't worked on very large projects yet.

let's say you have to write the front-end for an application, and you have to use several libraries from external parties. only problem: your code must be finished on august 4th, must be completely functional, tested and presented to your client, and the developers who are responsible for those external libraries, don't start writing/implementing them until october.

so, you write your code on their interfaces, and make your code so, that later on all you need to change is one line in a properties or xml file, rather than to rewrite/recompile your entire application.

Member Avatar for iamthwee

^^ That is an excellent example stultuske, I never got interfaces and most of the time I just simply ignore them (PHP) but your example shows how they can be useful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.