• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need of Interface In java

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One reason of using Interfaces in jave Is "Multiple Inheritence".
I m looking for the Second reason.........
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic