Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 5th, 2007, 4:02 AM   #1
pushkarajthorat
Java Developer
 
pushkarajthorat's Avatar
 
Join Date: Jun 2006
Location: Solapur, India.
Posts: 22
Rep Power: 0 pushkarajthorat is an unknown quantity at this point
Send a message via Yahoo to pushkarajthorat
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
__________________
[Pushkaraj]


Imagination is more important than knowledge – Albert Einstein
pushkarajthorat is offline   Reply With Quote
Old Jun 5th, 2007, 2:41 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Get rid of the ",a" in the first snippet.

class c implements a,a
titaniumdecoy is offline   Reply With Quote
Old Jun 6th, 2007, 2:18 AM   #3
pushkarajthorat
Java Developer
 
pushkarajthorat's Avatar
 
Join Date: Jun 2006
Location: Solapur, India.
Posts: 22
Rep Power: 0 pushkarajthorat is an unknown quantity at this point
Send a message via Yahoo to pushkarajthorat
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.
__________________
[Pushkaraj]


Imagination is more important than knowledge – Albert Einstein
pushkarajthorat is offline   Reply With Quote
Old Jun 6th, 2007, 2:30 AM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
If a class implements interface I, its subclasses also implement interface I. It makes no difference whether these classes explicitly implement interface I.
titaniumdecoy is offline   Reply With Quote
Old Jun 7th, 2007, 7:33 PM   #5
physicist
Hobbyist Programmer
 
Join Date: Oct 2006
Posts: 146
Rep Power: 2 physicist is on a distinguished road
in short, you get nothing extra. might as well not specify em
physicist is offline   Reply With Quote
Old Jun 9th, 2007, 10:31 AM   #6
msi_333
Newbie
 
msi_333's Avatar
 
Join Date: May 2007
Location: Egypt
Posts: 19
Rep Power: 0 msi_333 is on a distinguished road
Send a message via Yahoo to msi_333
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 .
msi_333 is offline   Reply With Quote
Old Jun 9th, 2007, 3:08 PM   #7
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
Quote:
Originally Posted by pushkarajthorat View Post
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.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jun 9th, 2007, 3:44 PM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by pegasus001 View Post
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.
titaniumdecoy is offline   Reply With Quote
Old Jun 9th, 2007, 3:47 PM   #9
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
It all makes sense now.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jun 9th, 2007, 5:44 PM   #10
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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
Implementing a dynamic class fhslacrosse13 C++ 6 Nov 1st, 2006 3:56 PM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 7:40 AM
C# Pilot Program Haz Existing Project Development 9 May 2nd, 2005 9:27 AM
creating class objects at run time garr79 C++ 3 Feb 8th, 2005 10:58 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:27 PM.

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