jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Need of Interface In java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need of Interface In java" Watch "Need of Interface In java" New topic

Need of Interface In java


Joined: Dec 25, 2007
Posts: 2
One reason of using Interfaces in jave Is "Multiple Inheritence".
I m looking for the Second reason.........
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
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.

Joined: Dec 25, 2007
Posts: 2
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

Joined: Jan 10, 2002
Posts: 63843


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.

JavaRanch Sheriff

[Asking smart questions] [About Bear] [Books by Bear]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
You've already given the reason. Why do you assume there must be a second reason? The first reason is good enough, I think.

"I'm not back." - Bill Harding, Twister
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Need of Interface In java
It's not a secret anymore!