Top Companies hiring on Shine.com. Click Here to apply !
Click Here to Submit Your Resume for MNC Jobs !
JavaKen.com

Go Back   JavaKen.com > Java Introduction > Object Oriented Concepts
Forgot User Name / Password ?


Reply
 
Thread Tools Display Modes
  #1  
Old 11 October 2008, 01:55 PM
Basanta_Sahoo Basanta_Sahoo is offline
Junior Member
 
Join Date: 07 Oct 2008
Location: Now in Mumbai.
Posts: 2
Question If two Interfaces have same Method, how to handle that Method in a Class implementing these two Interfaces ?

Click Here To Register & Submit Your Resume !

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?

Click Here To Register & Submit Your Resume !
Reply With Quote

  #2  
Old 14 October 2008, 10:54 AM
abdulkhader101 abdulkhader101 is offline
Junior Member
 
Join Date: 27 Aug 2008
Posts: 2
Thumbs up 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 !
Reply With Quote
  #3  
Old 21 November 2008, 04:56 PM
rameshrjha rameshrjha is offline
Junior Member
 
Join Date: 12 Sep 2008
Posts: 5
Default Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

Quote:
Originally Posted by Basanta_Sahoo View Post
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 metod. is it call to both methods of both 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...
Reply With Quote
  #4  
Old 22 November 2008, 08:38 PM
Winucharan Winucharan is offline
Junior Member
 
Join Date: 03 Nov 2008
Posts: 6
Arrow Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

Quote:
Originally Posted by abdulkhader101 View Post
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 !
any way we r only providing the abstract methods.so nothing happens.we can just invoke the methods by using the object references
Reply With Quote
  #5  
Old 29 July 2009, 07:07 PM
karthik_it2004 karthik_it2004 is offline
Junior Member
 
Join Date: 06 Jul 2009
Posts: 3
Blog Entries: 1
Default 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/Compile...ques_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();
}
}
Reply With Quote
  #6  
Old 05 August 2009, 03:52 PM
anup.lath anup.lath is offline
Junior Member
 
Join Date: 05 Aug 2009
Posts: 3
Default 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
Reply With Quote
  #7  
Old 06 August 2009, 02:34 PM
roshan_r007 roshan_r007 is offline
Junior Member
 
Join Date: 22 Aug 2008
Posts: 2
Thumbs down Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

Quote:
Originally Posted by anup.lath View Post
In java one class can implement only one interface because java doesnot support multiple inheritance
Hey ANUP LATH, it's not multiple interfaces, it's multiple classes.
Reply With Quote
  #8  
Old 10 August 2009, 10:02 PM
anitha.mca's Avatar
anitha.mca anitha.mca is offline
Member
 
Join Date: 23 Jun 2009
Location: Bangalore
Posts: 51
Default Re: If two Interfaces have same Method how can we implement that Method in Class implementing these two Interfaces ?

Quote:
Originally Posted by anup.lath View Post
In java one class can implement only one interface because java doesnot support multiple inheritance
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!
__________________
Ani
Reply With Quote
  #9  
Old 10 August 2009, 10:14 PM
anitha.mca's Avatar
anitha.mca anitha.mca is offline
Member
 
Join Date: 23 Jun 2009
Location: Bangalore
Posts: 51
Default Re: If two Interfaces have same Method, how to handle that Method in a Class implementing these two Interfaces ?

Quote:
Originally Posted by Basanta_Sahoo View Post
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?
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.
__________________
Ani
Reply With Quote
  #10  
Old 14 August 2009, 09:46 PM
soumya866 soumya866 is offline
Junior Member
 
Join Date: 12 Aug 2009
Posts: 25
Thumbs up Re: If two Interfaces have same Method, how to handle that Method in a Class implementing these two Interfaces ?

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
}
}
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is a Collection Interface ? vbraojava Generics and Collections 0 18 September 2008 01:59 PM
What we must follow to implement Interface ? vbraojava Object Oriented Concepts 0 12 September 2008 05:04 PM
What is an Interface ? vbraojava Object Oriented Concepts 0 12 September 2008 04:50 PM
What is Cloneable Interface ? vbraojava Cloning 0 08 September 2008 05:01 PM


All times are GMT +5.5. The time now is 04:53 AM.


Powered by vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright © 2008 ChetanaS, All Rights Reserved.