If two Interfaces have same Method, how to handle that Method in a Class implementing these two Interfaces ?

Discussion in 'Object Oriented Concepts' started by Basanta_Sahoo, Oct 11, 2008.

  1. Basanta_Sahoo

    Basanta_Sahoo New Member

    Suppose i have two interfaces having same method say A(). when i implement these two interfaces in a class, then what will happen when i call to the method. is it call to both methods of both interfaces?
     

  2. abdulkhader101

    abdulkhader101 New Member

    Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

    you are just providing the implementation to the method in the class. When you will have the same method in both the interfaces ? when you need the same implementation for all the classes which implement the ifaces. so in either of the cases if you need some other implentation then you will define the other method in interfaces. it doesnot give compiletime error also ? it is fine and correct . try it !
     
  3. rameshrjha

    rameshrjha New Member

    Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

    why to think so much... anyway after implmntation you are going to write your own logic in that method... so whatevr you will write ... that will be executed... :)
     
  4. Winucharan

    Winucharan New Member

    Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

    any way we r only providing the abstract methods.so nothing happens.we can just invoke the methods by using the object references
     
  5. karthik_it2004

    karthik_it2004 New Member

    Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

    If you wonder how it works, a good place to start is the "Dragon book" (Compilers: principles, compilers and tools, http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques_and_Tools).
    If you wonder why it works, this one is worth reading: http://java.sun.com/docs/books/jls/index.html

    interface one{
    public void Color();
    }
    interface two{
    public void Color();
    }
    public class TwoInterfaceSameMethod implements one,two {
    public void Color(){
    System.out.println("Common to all");
    }
    public one Choose(one o){
    return new one(){
    public void Color(){
    System.out.println("Color One");
    }
    };
    }
    public two Choose(two o){
    return new two(){
    public void Color(){
    System.out.println("Color Two");
    }
    };
    }
    }




    public class MainForTwoInterfaceSameMethod {

    public static void main(String[] args) {
    TwoInterfaceSameMethod c= new TwoInterfaceSameMethod();
    c.Color();
    one o= new TwoInterfaceSameMethod();
    two t= new TwoInterfaceSameMethod();
    o=c.Choose(o);
    t=c.Choose(t);

    o.Color();
    t.Color();
    }
    }
     
  6. anup.lath

    anup.lath New Member

    Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

    In java one class can implement only one interface because java doesnot support multiple inheritance
     
  7. roshan_r007

    roshan_r007 New Member

    Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

    Hey ANUP LATH, it's not multiple interfaces, it's multiple classes.
     
  8. anitha.mca

    anitha.mca New Member

    Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

    Java doesn't support multiple inheritance that is extending more than 1 class,a way to provide mulitple inheritance is this Interface inheritance,we can implement more than 1 interface.

    Hope this helps u!
     
  9. anitha.mca

    anitha.mca New Member

    To point out my view :

    Example :
    Code:
    interface A{   
        public void a();   
    }   
      
    interface B{   
       public void a();   
    }   
      
    class Test implements A,B{   
        public void a(){   
        System.out.println("This works!");   
        }   
        public int a(){   
        System.out.println("This does not!");   
        }   
    } 
    If you try this compiler will throw error ,

    Compiler says : a() is already defined

    We get this error due to rules of overloading method (overloading = declare two/more methods with the same name in the class).

    If you overload method, methods with the same name have to have different signatures (different parameters list).

    You cannot declare inside class two methods with the same name and the same parameter list, compiler will always complain.

    The same example with different method signature will work for this scenario.

    Hope this helps.
     
  10. soumya866

    soumya866 New Member

    case 1:
    If 2 interfaces contains a method with same signature and same return type we have to provide only one method implementation in the implemented class
    ex:
    interface left
    {
    public void m1()
    }
    interface right
    {
    public void m1()
    }
    class Test implements left,right
    public void m1()
    {
    ----
    }
    ----
    }
    CASE 2:
    IF 2 INTERFACES CONTAINS A METHOD WITH SAME METHOD NAME BUT DIFFERENT PARAMETERS THEN WE HAVE TO PROVIDE IMPLEMENTATION FOR BOTH METHODS IN THE IMPLEMENTED CLASS. THESE 2 METHODS ACTS AS OVERLOADED METHODS.
    EX2:
    interface left
    {
    public void m1()
    }
    interface right
    {
    public void m1(int i)
    }
    class Test implements left,right
    public void m1()
    {
    ----
    }
    public void m1(int i)
    {
    --
    }
    ---
    }
    CASE 3:
    IT IS IMPOSSIBLE TO IMPLEMENT TWO INTERFACES SIMULTANEOUSLY IF BOTH INTERFACES CONTAINS A METHOD WITH SAME SIGNATURE BUT DIFFERENT RETURN TYPES
    EX3:
    interface left
    {
    public void m1()
    }
    interface right
    {
    public int m1(int i)
    }
    class Test implements left,right
    public void m1()
    {
    ----
    }
    public int m1(int i)
    {
    --
    }
    ---
    }
    INTERFACE VARIABLE NAMING CONFLICTS:
    WE CAN RESOLVE INTERFACE NAMING CONFLICTS BY USING INTERFACE NAMES
    EX:
    interface left
    {
    int i=10;
    }
    interface right
    {
    int i=100;
    }
    class Test implements left,right
    {
    public static void main(string args[])
    {
    System.out.println(i);///// Compile time error reference to i is ambiguous
    System.out.println(left.i);//10 will be printed
    System.out.println(right.i);//100 will be printed
    }
    }
     
  11. soumya866

    soumya866 New Member

    Correct me if I am wrong.............:)
     
  12. anoop.chiliveri

    anoop.chiliveri New Member

    If two Interfaces have same Method, how to handle that Method in a Class implementing these two Interfaces ?
    please Give me solution for this with an example
     
  13. urpal_priya

    urpal_priya New Member

    Hi Soumya,

    I like ur explanation. That was great.

    Priya.
     
  14. padamalacharan

    padamalacharan New Member

    Thanx for All the members replied for this.
    My spl thanx to karthik and soumya for the better explanation.
     
    Last edited: Nov 23, 2009
  15. rajbharath

    rajbharath New Member

    1) I think that if a class implements two interfaces with same methods we need to write only one method in a class body.If interfacess have diff signature we can write two methods

    interface A
    { public void mone();
    }
    interface B
    { public void mone();
    }
    public class SameMethod implements A,B
    {
    public void mone();
    }
     
    Last edited: Feb 12, 2010
  16. rajbharath

    rajbharath New Member

    what is the use of callablestatements in jdbc

    i want ans for that
     
  17. rajbharath

    rajbharath New Member

    Hi sowmya ur correct, ur explanation is excellent. karthik u too
     
  18. rajbharath

    rajbharath New Member

    If we call a method using classname it belongs to STATICMETHOD.Then why we call class.forName() as factory method

    any one have idea about this try to ans it:)
     
    Last edited: Feb 12, 2010
  19. Gyanendra

    Gyanendra New Member

    gr8 alot of thanx for Wonderful Explation

    rgds,
    gyan
     
  20. rameshcharykotha

    rameshcharykotha New Member

    no need to implement two methods explicitly even if you do that also it will raise an error saying duplicate methods, just implement one method that is common for all interfaces and call that method.
     

Share This Page