Results 1 to 10 of 10

Thread: interface

  1. #1
    makrandubale is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default interface

    hi, if a class implements two interfaces, Interface1 and Interface2, and both the interfaces have a method with same signature then no matter what reference (the class or any of the interfaces) i use to invoke the method, the same method will be executed. can some one give an ex. which can explain the benifits of implementing two interfaces with the same method signatures.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by makrandubale View Post
    hi, if a class implements two interfaces, Interface1 and Interface2, and both the interfaces have a method with same signature then no matter what reference (the class or any of the interfaces) i use to invoke the method, the same method will be executed. can some one give an ex. which can explain the benifits of implementing two interfaces with the same method signatures.
    There is no benefit, the compiler just can't forbid you to do so. Try to circumvent such a situation by renaming one of the methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    makrandubale is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks for the reply Jos. but don't you think, that java allows such kinda implementation leaving the developer in confusion whereas C++ is strict about not allowing such kinda implementation.

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by JosAH View Post
    Tjust can't forbid you to do so.
    "can't forbid" means "to allow". is that really what you mean?

  5. #5
    makrandubale is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    the complier allows it.. so we can conclude as YES

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by makrandubale View Post
    the complier allows it.. so we can conclude as YES
    Indeed; the language would need to be changed to 'fix' this, i.e. the method overloading mechanism needs to be changed, additional keywords or whatever need to be defined all for this. It is much easier and cheaper to allow a single method implementation to 'fit' two or more interfaces. Java is not C++.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    makrandubale is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    ya, java tried to solve the C++ diamond problem (for the compiler), and left the decision on the developers.... good busniess logic :)

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by makrandubale View Post
    ya, java tried to solve the C++ diamond problem (for the compiler), and left the decision on the developers.... good busniess logic :)
    That's all because multiple inheritance of implementation and Java doesn't allow that. There are already too many ugly dark corners in Java (you just found one).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    makrandubale is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    hye.. can you share some more similar things?

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,532
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by makrandubale View Post
    hye.. can you share some more similar things?
    Similar thing ... my pet peeve complaint is that one isn't allowed to refer to member variables of an object before the object is constructed (which is understandable) while it is allowed to use methods called by a superclass constructor that can be overridden by a subclass. I understand why it happens (simplicity of the vtab at construction time) but I just don't like it ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Need help with Interface
    By karanhs6 in forum New To Java
    Replies: 18
    Last Post: 12-14-2010, 09:47 AM
  2. help me about Interface
    By manhtungtnk28@gmail.com in forum New To Java
    Replies: 3
    Last Post: 11-23-2009, 05:15 PM

Posting Permissions

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