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.
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.
0
Thanks for such a great answer.