GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 205 of 493    Print  
let say there are two interfaces like the following

interface int1{
int meth1();
}

interface int22{
int meth1();
}

ok then

i am implementing both of them in a calss as follows

class 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

  
Total Answers and Comments: 13 Last Update: August 12, 2007     Asked by: Y Niranjan Prasad 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
March 16, 2006 09:39:19   #1  
juilipanse Member Since: February 2006   Contribution: 2    

RE: let say there are two interfaces like the followin...

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


 
Is this answer useful? Yes | No
March 17, 2006 09:38:35   #2  
Yalisetty Niranjan Prasad        

RE: let say there are two interfaces like the followin...

yes,

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

ok


 
Is this answer useful? Yes | No
March 18, 2006 08:26:34   #3  
Yalisetty Niranjan Prasad        

RE: let say there are two interfaces like the followin...

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?


 
Is this answer useful? Yes | No
March 30, 2006 09:30:17   #4  
shahid        

RE: let say there are two interfaces like the followin...
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
 
Is this answer useful? Yes | No
April 22, 2006 14:49:20   #5  
Michael Wei        

RE: let say there are two interfaces like the followin...
That's is correct. That's exactly why Java allows multiple implementation but not multiple inheritence
 
Is this answer useful? Yes | No
April 26, 2006 05:29:48   #6  
kiran        

RE: let say there are two interfaces like the followin...

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.


 
Is this answer useful? Yes | No
April 26, 2006 05:59:59   #7  
kiran kumar        

RE: let say there are two interfaces like the followin...

. 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


 
Is this answer useful? Yes | No
May 02, 2006 05:45:06   #8  
shivalak Member Since: May 2006   Contribution: 9    

RE: let say there are two interfaces like the followin...
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.}
 
Is this answer useful? Yes | No
May 02, 2006 08:13:35   #9  
shivalak Member Since: May 2006   Contribution: 9    

RE: let say there are two interfaces like the followin...
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.

 
Is this answer useful? Yes | No
May 17, 2006 15:11:27   #10  
NagaRaju        

RE: let say there are two interfaces like the followin...

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


 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape