Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-27-2007, 04:41 PM
Member
 
Join Date: Jul 2007
Posts: 8
imran_khan is on a distinguished road
Interfaces
Hi ,
This is my first question to java-forums.Can you tell me exactly why we use interfaces if we can use inheritance by using the concept of subclasses i.e. reusability.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-27-2007, 07:53 PM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Assume that you need your class to inherit from two or more classes. Java's "extends" keyword allowed to be used for only one class. The solution to this is to have one class but multiple interfaces if you need more than one base class. This is implemented like this because it is observed that multiple inheritance (extending from more than one class) has some problems.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-27-2007, 09:13 PM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Just adding, interfaces in most java literature are often considered as a capability to be able to do things, so implementing interfaces doesn't exactly the same as inheriting the method of a superclass, since in an interface, the method does nothing, only declaration in the form of abstract method

Some problem that occur when using multiple inheritance (according to some references), if classA and classB got the same method name test() but do very diffrent things, and then classC inherits both of the class but does not override the method test(), when an instance of classC would call the method test() it would be a problem to decide whether the instance would use the classA's test() or classB's test()

Interface is an abstract class, so the class that implements the interface must override the method that it inherited, thus, preventing the multiple inheritance problem above

Another advantages of using interfaces are the availability to use polymorphism to these classes, even if they don't relate at all
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-28-2007, 12:57 AM
Senior Member
 
Join Date: Dec 2006
Posts: 748
levent is on a distinguished road
Good description cruxblack. Green reputation is sent
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-28-2007, 06:54 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Thank u Mr.levent
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-30-2007, 10:11 AM
Member
 
Join Date: Jul 2007
Posts: 8
imran_khan is on a distinguished road
Thanx Mr. cruxblack I understand ur explanation.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
interfaces.. sireesha New To Java 5 01-16-2008 07:52 PM
Interfaces Kavana Krishnappa New To Java 7 12-11-2007 06:28 PM
Using interfaces with Delegation Java Tip Java Tips 0 12-06-2007 03:49 PM
Using Interfaces JavaForums Java Blogs 0 11-08-2007 03:30 PM
Help, someone clear up Interfaces for me mathias New To Java 1 08-06-2007 04:26 AM


All times are GMT +3. The time now is 08:04 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org