Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Implementing same interface multiple time in herarchie of class (http://www.programmingforums.org/showthread.php?t=13281)

pushkarajthorat Jun 5th, 2007 5:02 AM

Implementing same interface multiple time in herarchie of class
 
I dont understand why we need to implement same interface through the herarchie of class

example :

:

interface a {
}

class c implements a,a
{

}

gives compilation error : Duplicate interface a for the type c


but
:

interface a {
}

class c implements a
{

}
class d extends c implements a
{

}


is working fine

titaniumdecoy Jun 5th, 2007 3:41 PM

Get rid of the ",a" in the first snippet.

:

class c implements a,a

pushkarajthorat Jun 6th, 2007 3:18 AM

OK i think I have to make it more clear

Is there any good reason where we need to implement the interface in a class which is already implemented in some of its superclasses...

If you are giving the reason as, "if we have to change the functionality", then we could simply override function insted.

and if reason is, "If we have to get a reference of interface pointing the class object" then its already implementing the interface in superclass.

but I want to know that what we get extra if we implement the interaface, in subclass.

titaniumdecoy Jun 6th, 2007 3:30 AM

If a class implements interface I, its subclasses also implement interface I. It makes no difference whether these classes explicitly implement interface I.

physicist Jun 7th, 2007 8:33 PM

in short, you get nothing extra. might as well not specify em

msi_333 Jun 9th, 2007 11:31 AM

It is a design trick
 
As you know ,interfaces are with no any implementation , we often make the implementation of it in subclass rather than super class .Because it will be needed only in this subclass for example
if we have a super class X and two sub classes Y & Z, now if we implement the interface in X both Y ,Z will also inherit it ,But it will be not a good OOP design if class Y need this interface and class Z don't.So in this case to make it more simple we make the implementation in the subclass Y only and this will decrease the lines of code and make it easier to understand, .So it is a design trick and depends on your software engineering
work! . I hope what i wrote is useful to you my friend and good luck .:D

pegasus001 Jun 9th, 2007 4:08 PM

Quote:

Originally Posted by pushkarajthorat (Post 128754)
I dont understand why we need to implement same interface through the herarchie of class

example :

:

interface a {
}

class c implements a,a
{

}

gives compilation error : Duplicate interface a for the type c


but
:

interface a {
}

class c implements a
{

}
class d extends c implements a
{

}


is working fine

But will it work if you specify some methods in the interface a??? Because you have to implement two times the same methods, and it doesn't concern overloading. I don't see why you would do that.

titaniumdecoy Jun 9th, 2007 4:44 PM

Quote:

Originally Posted by pegasus001 (Post 128959)
But will it work if you specify some methods in the interface a??? Because you have to implement two times the same methods, and it doesn't concern overloading. I don't see why you would do that.

Actually, it does concern overloading, and you do not have to implement the same methods more than once.

Imagine a non-abstract class A which implements interface I. As class A is not abstract, it must provide an implementation for any methods declared in I.

Now imagine class B extends class A. Class B need not implement any of the methods declared in interface I as the implementations for those methods are inherited from its superclass A.

However, if class B does provide implementations for some or all of the methods declared in I, those methods will take precedence over those defined in class A. In other words, when a method is called on an object, the VM looks for a definition first in that class, then in its superclass, and continues up the class hierarchy until an implementation is found.

I recommend this problem from my CS AP class in high school. It covers all of this and may be easier to understand.

pegasus001 Jun 9th, 2007 4:47 PM

It all makes sense now.

lectricpharaoh Jun 9th, 2007 6:44 PM

To answer the OPs question, I think the reason that the implementation of an interface is made explicit when it's already implicit (due to a superclass implementing it) is to make things more clear. It is a good design idea, as it makes it easier for the programmer to determine if a given interface is implemented by a particular class (no need to check the hierarchy of classes). Functionally, though, it makes no difference.


All times are GMT -5. The time now is 2:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC