Results 1 to 6 of 6

Thread: Interfaces

  1. #1
    imran_khan is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Unhappy 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.

  2. #2
    levent Guest

    Default

    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.

  3. #3
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    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

  4. #4
    levent Guest

    Default

    Good description cruxblack. Green reputation is sent ;)

  5. #5
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Thank u Mr.levent:D

  6. #6
    imran_khan is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

Similar Threads

  1. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 05:52 PM
  2. Interfaces
    By Kavana Krishnappa in forum New To Java
    Replies: 7
    Last Post: 12-11-2007, 04:28 PM
  3. Using interfaces with Delegation
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 01:49 PM
  4. Help, someone clear up Interfaces for me
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •