0

Can any body tell me more about Interfaces in java?

I have some doubts with interfaces for example :- 1)what is use of interfaces. 2) can interfaces contain variables? 3)how interfaces can be use for just practice. Please answer this question I am really confused. Thanks for reading.

22nd Dec 2016, 1:27 PM
nayan
nayan - avatar
2 Answers
+ 1
Interfaces provide a way to define a high level description of some feature. An example of a feature in the language is the Comparable interface which says that any type that implements it can be compared to another instance in a way that is similar to checking if they're greater than, equal to, or less than another instance. This can be really helpful for just saying what a class should look like without actually writing the code for doing so. You'll often find that there is more than one way to do something and you don't want to write the same boilerplate code for that which is why interfaces are helpful. Streams in Java are a great example of this. Interfaces define what methods a class should have. They do not define any variables. In most versions of Java you cannot define an implementation of any method inside the interface however Java 8 did add the ability to create default implementations in interfaces. This was done to decrease the number of breaking changes an interface might see. If you want to see interfaces in action you should really look at the JDK itself. It is full of real interfaces and their implementations. Namely start with the Comparable interface and Streams as mentioned previously.
22nd Dec 2016, 2:26 PM
James Durand
James Durand - avatar
0
Thanks for such a great answer.
24th Dec 2016, 5:43 AM
nayan
nayan - avatar