• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need of Interface In java

 
vijay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One reason of using Interfaces in jave Is "Multiple Inheritence".
I m looking for the Second reason.........
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The most important reason for using interfaces is to be able to hide the implementation. This way you can provide someone with your interface without him/her having to know what goes on 'behind the scenes'. This means that you can change your implementation without affecting the other person. It also allows for some degree of privacy.
 
vijay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Petrus.
The same can be achieved by the abstract class then what functionality does Interface provides that cant be achieved with an Abstract class.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"vijay",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've already given the reason. Why do you assume there must be a second reason? The first reason is good enough, I think.
 
Petrus Pelser
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where design comes in. I like to think that the difference between abstract classes and interfaces is the following:

With abstract classes you create abstractions (as the name implies). This means that you can (using the old car example) define a Car class as abstract, meaning that you know what a car is and what it should do. We all know what a car is, but you can not instantiate, i.e build a car (you will just end up with the framework). You can build a BMW 320i though.

Concerning interfaces, I like to think of them as special abilities. You are a subclass of the abstract class Person. But that is not all, you also play cricket and bowl very well, something not every person can do. Thus you can implement the Bowler interface, but you still inherit from Person.

You may think of this as multiple inheritance and also misuse this by implementing multiple inheritance in this way, but that's just not cricket! That's a hack instead of a good design and that is not what it was meant to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic