Java Interfaces: Mystery Unfolds
Today we are going to throw some light on a very important concept of Java or what I call it as “The Backbone of Java” .The name is “Interfaces”.
Through this article I will try to break some myth (yes myth exist,believe it or not) or may be some different understandings that I found very common around the interfaces in Java.(Note that I am not challenging anyone,just writing my theory in this article,what I believe).
First of all, I start with a simple Question —
What is an Interface? What is the need of Interface in Java or What is its use ?
Frankly speaking I ask this question to everyone who knows Java. Some points which I found very common in the answers are-
1)Interfaces are used to make full abstraction.
2)Interface help us to achieve Multiple Inheritance in Java because we don’t get ambiguity error in cases of Interfaces (like in C++ we get Diamond Problem)
3)Interface contain all abstract method as opposed to abstract classes which may/may not contain abstract methods.
— — — -
My friend, I think these are not the correct answers for an Interface.(Again I am not challenging any college or school teacher or any senior professional)
First of all,get the point into your mind that “JAVA DOESN’T SUPPORT MULTIPLE INHERITANCE”.Don’t argue that it do support multiple inheritance via Interfaces(that’s not correct,believe me if you want to believe).
Secondly, don’t say that Java solves C++ ambiguity error problem by using Interfaces. Just sit and think if C++ can solve the ambiguity error problem using Scope Resolution operator(::) why can’t Java do that.Why Java introduce the whole new concept of Interfaces which become a problem in the situation when you have multiple methods in an Interface and want to implement only one[Reason to introduce Adapter Classes in AWT]…..(Oh Please,don’t say that Java doesn’t have Scope Resolution,if they want to support it,they could introduce at the time of creating Java.
My Dear Friend,We do have Scope Resolution operator(::) in Java
— — — —
WHAT INTERFACE ACTUALLY IS ?
Now,jump right away into the world of Interfaces.Interfaces are the “contracts” between the class and the outside world.They are used to make contract between two entities.Whenever you have to define some contract kind of a thing b/w two parties in Java,use Interfaces.
— — — — — — -
Time to break the myth…
Have a seat and fasten your seat belt ,Relax.May be you are one of those who are not going to believe me about this….
In Java,you can make methods which are having body(Oh! Just wait what is written here,are you serious) .Yes,Java8 gives you that flexibility ☺.From Java8,you can make two kinds of methods(static methods and default methods) which are having body.Java doesn’t solves the multiple inheritance ambiguity problem of C++.When you define the default methods in interface having body,then their may be a chance of getting compile time ambiguity error.Take a look at following code :
(Bust The Myth) —
1)Interfaces can have methods with body.
2)we can get ambiguity error at compile time).
3)Interfaces are not designed to achieve Multiple Inheritance in Java.
Hold On,Its not over yet.One Big Interface Missile is on the launchpad with Java SE9 scheduled to be released in July this year -> “private methods having body in a Java Interface.”(Yes,you read it correct,private methods in an interface)
(I have test it with JDK9 beta release,most probably it will make it to final release as well.)
I Hope you get something out of this article.Have a nice time..Any Suggestions are welcome,If you like this do share,comment and love my article.
Question :Why do Java Programmers wear glasses ?
Answer : Because they don’t see sharp(C#).
[YoUr TiMe Is PrEcIoUs , DoNt WaStE iT iN ReAdInG mY aRtIcLeS.]