Let say there are two interfaces like the followinginterface int1{int meth1();}interface int22{int meth1();}ok theni am implementing both of them in a calss as followsclass mycl implements int1,int2{}now as the rule says, if a class implements an interface it must complete its signature.now my questions is that if i define meth1(), as that method is in both interface, may i know how can i define both of that methods.please answer me

Showing Answers 1 - 17 of 17 Answers

juilipanse

  • Mar 16th, 2006
 

I think there is no way to define the same method twise..

Consider this as example...

interface Interface1
{
 int method();
}

interface Interface2
{
 int method();
}


public class  Question1 implements Interface1, Interface2
{
 public int method()
 {
  System.out.println("From implemented method()...");
  return 0;
 }

 public static void main(String[] args)
 {
  Question1 obj = new Question1();
  obj.method();
 }
}

But if you find that we can do it in any other way please let me know...!

  Was this answer useful?  Yes

Yalisetty Niranjan Prasad

  • Mar 17th, 2006
 

yes,

i posted the question and forgot to see it. but now i will try bye real program and post it.

ok

  Was this answer useful?  Yes

Yalisetty Niranjan Prasad

  • Mar 18th, 2006
 

yes,

you are correct, there is no way to define the method twice. and it doesnt matter which method we define because we want the method with that name and the compiler dont bother about in which interface it comes from, it checks only that the method is defined or not. am i correct?

  Was this answer useful?  Yes

shahid

  • Mar 30th, 2006
 

Question: let say there are two interfaces like the followinginterface int1{int meth1();}interface int22{int meth1();}ok theni am implementing both of them in a calss as followsclass mycl implements int1,int2{}now as the rule says, if a class implements an interface it must complete its signature.now my questions is that if i define meth1(), as that method is in both interface, may i know how can i define both of that methods.please answer me

  Was this answer useful?  Yes

Michael Wei

  • Apr 22nd, 2006
 

That's is correct. That's exactly why Java allows multiple implementation but not multiple inheritence

  Was this answer useful?  Yes

kiran

  • Apr 26th, 2006
 

interface Interface1
{
 int method();
}

interface Interface2
{
 int method();
}


public class  Question1 implements Interface1, Interface2
{
 public int method()
 {
  System.out.println("From implemented method()...");
  return 0;
 }

 public static void main(String[] args)
 {
  Question1 obj = new Question1();
  obj.method();
 }
}

Hi pansi, This will work fine. It daisplay's the result.

  Was this answer useful?  Yes

kiran kumar

  • Apr 26th, 2006
 

. For example,

 interface Interface1
  {
   int method();
   }
interface Interface2
  {
   int method();   
  }
class  Question1 implements Interface1, Interface2
  {
     public int method()
    {
    System.out.println("From implemented method()...");
    return 0;
    }
    public static void main(String[] args)
   {
   Question1 obj = new Question1();
   obj.method();
    }
  }

 This will run fine and the following result it will display?..
 output :
 
From implemented method()...

If it contains same method with different signatures. Then it will give compile time error.

 

For example,

interface Interface1
  {
   int method();
   }
interface Interface2
  {
   int method();   
  }
class  Question1 implements Interface1, Interface2
  {
     public int method()
    {
    System.out.println("From implemented method()...");
    return 0;
    }
    public static void main(String[] args)
   {
   Question1 obj = new Question1();
   obj.method();
    }
  }

 

Output:

 

Question1.java:14: method() in Question1 cannot implement method() in Interface2

; attempting to use incompatible return type

found   : int

required: float

 public int method()

            ^

1 error

  Was this answer useful?  Yes

shivalak

  • May 2nd, 2006
 

Hi , I dont see any benefit in doing like this and its not a good design too :( I would recommend to change your design something like this.interface int1{ //Eg. Place some methods which will be exposed to the third partyint meth1();}interface int22 extends int1{// Some other methods (but not meth1) which will be used for the internal activities.int meth2();}then implement your class like thisclass mycl implements int22{ // Implement both meth1 and meth2 here.}

  Was this answer useful?  Yes

shivalak

  • May 2nd, 2006
 

Hi ,
I dont see any benefit in doing like this and its not a good design too :(
This can be changed something like the following.

interface int1 {
//Eg. Place some methods which will be exposed to the third party
int meth1();
}
interface int22 extends int1 {
// Some other methods (but not meth1) which will be used for the internal activities.
int meth2();
}
then implement your class like this
class mycl implements int22 {
// Implement both meth1 and meth2 here.
}

Cheers
Shiva.

  Was this answer useful?  Yes

NagaRaju

  • May 17th, 2006
 

yes u can do it with no problem,

by using anonymous classes u can implement this.

I am writing the code to ur problem.

interface int1{
int meth1();
}

interface int22{
int meth1();
}

public class Applic implements int1,int22

{

public int meth1(){

System.out.println("this is the method present in int1 interface implemented class;");

return 5;

}

// it is the one of the Syntax if u want to create a object to a class that extending int 22 interface.

static int22 i22=new int22(){

public int meth1()

{

System.out.println("this is the method developed in int 22");

return 6;

}

};

public static void main(String args[])

{

i22.meth1();//it calls the meth1 present in interface int22 implemented class.

Applic c=new Applic();

c.meth1();

}

I hope this is the right answer ,u have know better than this pls post

  Was this answer useful?  Yes

ramuatk

  • Jul 18th, 2006
 

if whenever two interfaces have same methods, may or may not be there.

if two interface have same method, if u implemented in one class both interface at that time java runtime enviranment or JVM searches both interfaces have any abstact methods have there or not if the methods are ther it will give the suggetion to programmer to implemets those methods, if two interfaces have same methods at that time it takes prefarence to first interface.

if u have any doughts send me a mail to me.

ramuatk@yahoo.com

always bee smiling..............

  Was this answer useful?  Yes

sandhya

  • Apr 1st, 2007
 

Hi ! 
  
yes! I have chcd the above program and not giving any errors in the sense that we can use the same method in the two diff interfaces but the implementation is in the main class so this will really work ! and the expected output is printed

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions