Wednesday, June 2, 2010

Interfaces in Java

What are Interfaces in Java???
In fact the question is
Why Interfaces???

I remember this was a question used to be there in my mind always during the days when I started teaching Core java initially.

And I always found misleaded by many of the books which says…

Ø Interfaces are alternative to multiple inheritance…
? Question is why multiple inheritance was removed from C++, if an alternative was to be provided…and if we revisit java evolution period we see that the ambiguity problem with C++ multiple inheritance was addressed in Java by removing it.
? And if it is true, then why two different keywords… extends for Base Classes and implements for Interfaces…
Ø Interfaces are classes.

Let me put my point of view now, and you are always welcome to come with your viewpoints as well on this.

After all my analysis and reiterating java and doing R&D in MRs, I come to believe the following facts about Interfaces:

Ø We can build a complete project even without creating a single interface of our own.
Ø The above point doesn’t mean that Interfaces are useless.
Ø Interfaces are very religiously used in Java – They are used as a tool to implement 100% level of Abstraction. Eg.
o 100% Level of Abstraction :: The following statement is a very good example of demoing Interfaces as a tool for Abstraction:
List obj=new LinkedList();
where List is an interface and LinkedList is a class implementing List interfaces… Collections API !!! Remember !
Try this and you will come to know that via obj only List methods will be available @ runtime. This clearly shows that obj here is used as an object of List interface to have limited access to methods of LinkedList class, which means abstraction is being implemented here and that too with 100% level as List is an interface and interfaces can’t contain their own method definitions.
o Hence Interfaces are alternative to abstract classes in order to provide 100% level of Abstraction.
o Behavioral Standardization of Class Hierarchy – In simpler terms a project manager can make use of interfaces in order to ensure that the classes being defined by the developers are meeting the client requirements. Remember, it becomes necessary to define all method bodies while implementing an Interface by a class.

I am pretty sure this would be a help to you all to understand “WHY Interfaces?” better and will be more able to demonstrate the same in your classes….

All the Best!!!

5 comments:

Sumit Thakur said...

Nice post sir........... Very much clarified on interfaces

smartlearner.in said...

Good One....
Interfaces are for...
1.. Pure Generalization
2.. Dynamic Polymorphism..(Memmory Utilization :) )

sunil verma said...

sir i want to know about,what is Interfaces in c#..

smartlearner.in said...

Interfaces are same whether in Java or C#..


I think So..

Abhijeet mukherjee said...

this knowledge is very use ful for me and i looking more from you