aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implementing an interface - Is it IS-A ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implementing an interface - Is it IS-A ?" Watch "Implementing an interface - Is it IS-A ?" New topic
Author

Implementing an interface - Is it IS-A ?

Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
Can the relationship between a class and the interface implemented by it be called IS-A ? Or is it only for pure inheritance ?

Thanks
Devi
Chandrasekhar kotha
Greenhorn

Joined: Jun 27, 2005
Posts: 22
I guess it only for inheritence purpose . coz Interface dosen't consits of any state..to say.. IS-A relation..

frns.welcome for ur comments...

thanks
~sekhar
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
what exactly is it that we inherit from an interface anyways
, a class extending an interface is only forced to provide a body to all the methods of the interface

please reply back for any corrections
Chandrasekhar kotha
Greenhorn

Joined: Jun 27, 2005
Posts: 22
Hi,

Interface contains only declarations of methods.. No definations.. When there is no defination..how do we say .. it maintains IS a relation ship..

folks what do u say??
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Hmm... i dont think so. As shekhar said IS-A holds for inheritance, similarly interface stands for behaviour . Two different objects communicate via an interface where as in an IS-A relationship an object actually inherits the properties of another.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Chandrasekhar kotha
Greenhorn

Joined: Jun 27, 2005
Posts: 22
Hey frns..

A class implements interface is called as realization.. as per UML.

Hope you got me..
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
what exactly is it that we inherit from an interface anyways
, a class extending an interface is only forced to provide a body to all the methods of the interface

Bhavesh! That is where the difference is, interfaces are always implemented never extended . When you say "implementing" then you just know you have to implement a behaviour, how? you are not aware of it, you may do whatever you want to implement it.
When you say "extending", you already have some idea of what you are going to do, you may either want to follow it or change/modify if you want to.
Sreedevi Vinod
Ranch Hand

Joined: Jan 17, 2005
Posts: 117
Thanks everyone.
So the relationship of a class with its interface cannot be called IS-A.
Is there any other term for describing such a relationship ?

Thanks
Devi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Devi]: Can the relationship between a class and the interface implemented by it be called IS-A ?

Yes, certainly.

[sekhar]; Interface dosen't consits of any state..to say.. IS-A relation..

An interface doesn't implement stateful behavior, but it certainly can reuire it of implementing classes. For example the Iterator interface requires implenting classes to remember which items in a collection have already been iterated over, and which have yet to be iterated over. Any Iterator must have state.

On the other hand, I'm not sure why this matters. State has nothing to do with the IS-A relationship. Where did this idea come from?

I suspect that the problem here is that many books talk about IS-A when they first describe the concept of inheritance, and this is usually before they talk about interfaces. Later when these books do describe interfaces, they don't talk much about IS-A since that's old news. But that doesn't mean that classes do not have an IS-A relationship with the interfaces that they implement. It just means that few people bother talking explicitly about the IS-A relationship after it's first introduced.

[bhavesh]: what exactly is it that we inherit from an interface anyways
, a class extending an interface is only forced to provide a body to all the methods of the interface


Aside from inheriting any constants and static nested classes or interfaces defined within the interface, a class inherits the method declarations from an interface it implements. It also inherits the full API of the interface (meaning, the verbal description of the behavior of a class or interface), and is obligated to obey it. This is not enforced by the compiler, but is subject to human interpretation. If you implement an interface but your class does not behave according to the description given in the interface's API, then you have lied to the other programmers who read the APIs of the class and the interface it "implements", and they will consider your work to be incorrect.

[Akhil]: interfaces are always implemented never extended

False - an interface can extend another interface.

[sekhar]: So the relationship of a class with its interface cannot be called IS-A.

No, this is not correct at all. An ArrayList IS A List, which IS A Collection. A Thread IS A Runnable. A String IS A CharSequence.


"I'm not back." - Bill Harding, Twister
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"Can the relationship between a class and the interface implemented by it be called IS-A ? Or is it only for pure inheritance?"

The "IS-A" relationship in essence means that anywhere a derived type may be used, a base type may be used. Now I know I am using the terms "derived" and "base", which are associated with Class inheritance, because I couldn't readily think of more generic terms that include interface inheritance.

Nonetheless, if you define the "IS-A" relationship to mean that anywhere type D may be used, type B may be used, then you have an "IS-A" relationship. Interface implementation then certainly creates an "IS-A" relationship.
[ June 28, 2005: Message edited by: J Borderi ]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
an interface can extend another interface.
Agreed Jim! I was waiting for it.

But friend, will "IS-A" relationship hold always.
say if we have a class myFrame extends JFrame and implements ActionListener

so myFrame IS-A JFrame... makes sense.Also myFrame IS-AN ActionListener??? Not certainly, how about myFrame has behaviour of ActionListener.

I guess, an "IS-A" relationship will hold even when you track down the whole hierarchy of inheritance, but that may not be the case with interfaces... two VERY DIFFERENT classes may implement same interface, but may never have a relation in common.
[ June 28, 2005: Message edited by: Akhil Trivedi ]
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"say if we have a class myFrame extends JFrame and implements ActionListener so myFrame IS-A JFrame... makes sense. Also myFrame IS-AN ActionListener??? Not certainly, how about myFrame has behaviour of ActionListener"

Perhaps you will have a better understanding when you realize that, in your scenario, "everywhere you may use an ActionListener you may use a myFrame."
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Akhil]: say if we have a class myFrame extends JFrame and implements ActionListener

so myFrame IS-A JFrame... makes sense.Also myFrame IS-AN ActionListener??? Not certainly,


Why not? A MyFrame is an ActionListener. Sure. I don't really like this sort of design because it imposes too many unrelated responsibilities on a single class - I'd rather make a separate class nested within MyFrame which handles the actionlistening responsibilities. But that's a design decision. If you choose to have MyFrame implement ActionListener, then it is an ActionListener.

how about myFrame has behaviour of ActionListener.

You can rephrase it if you like, but a MyFrame is an ActionListener.

two VERY DIFFERENT classes may implement same interface, but may never have a relation in common.

But they do have something in common - whatever methods the interface defines. If they're very different, that's probably a sign of poor design. You can create similarly poor designs extending a useless base class if you like. That doesn't prove or disprove much about the IS-A relationship - it just means that if a class or interface is poorly designed, then it can difficult to make meaningful statements about it.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531

"everywhere you may use an ActionListener you may use a myFrame."


"has behaviour of" does not mean you can completely replace it. Let me put another example all (oops sorry some) electrical appliances, would implement pluggable interface but that doesn't mean you can replace a fridge with a water-heater. Ofcourse, fridge will have a behaviour of being pluggable and at same time water-heater as well.

Further, same above quote will also contradict with "IS-A" relationship, won't it?? Have a look at this...

"everywhere you may use an ActionListener you may use a myFrame, because myFrame IS-AN ActionListener"
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
two VERY DIFFERENT classes may implement same interface, but may never have a relation in common. "But they do have something in common - whatever methods the interface defines."

Absolutely right! But "will IS-A relationship hold?" is the question... ok, ok ok... let me not mess-up with myself. I would like to learn one thing from you guys, can you tell me if an "IS-A relationship" should bound to hold in the hierarchy,

I mean,

A extends B -------> IS-A holds, also
C extends B -------> IS-A holds.

now, should there be implicit "IS-A relationship" between A and C??? If it the answer is "yes", then I would stick on to deny,
"interface = IS-A relationship"

[ June 29, 2005: Message edited by: Akhil Trivedi ]
[ June 29, 2005: Message edited by: Akhil Trivedi ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Akhil

I guess, in your example you wanted to put like,
B extends A,
C extends B.

Rather than,
A extends B,
C extends B

because in the hierarchy you have put , C IS-NOT-A A because both are extending B but it doens't mean that C IS-A A. C can be adding more behavior and things. C and A will be siblings/peer in that case not parent/child relation.

e.g.
1. Car (equiv. to B class in your latest example)
2. V6 engine car (equiv. to A class in your latest example)
3. V12 engine car (equiv. to C class in your latest example)

Here, V6 and V12 both are Cars BUT V6 and V12 are not parent/child in any sense.

If it was like a chain, B extends A, C extends B then C would have "implicitly" been having IS-A relation with A.

Regards
Maulin
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Akhil]:

A extends B -------> IS-A holds, also
C extends B -------> IS-A holds.

now, should there be implicit "IS-A relationship" between A and C???


No.

If it the answer is "yes", then I would stick on to deny,
"interface = IS-A relationship"


Since the answer was "no", will you stop denying it then?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
Good morning guys!
Thanks Maulin, I was clear in what I had mentioned, may be I could not fix the word "hierarchy" rightly.

Ya Jim, as said, I must stop denying it now. :-) Frankly speaking, I am UML illieterate. Hmm... so I hope it all comes like this...

interface --> IS-A relationship
inheritance --> IS-A relationship.
But interface is not inheritance.

Good thread, would like to explore more on this.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
IS-A is a language independent concept. Consider a fictional language Latrine that creates classes dynamically at runtime:

Boose = factory.create( Banana, Moose );

The language says that a Boose may now be passed to any method that accepts a Banana, Moose or a Boose. In other words, a Boose IS-A Boose and a Banana and a Moose.

A implementing interface Juice in Java IS-A Juice. The IS-A relationship has nothing to do with the mechanics of inheritence per se.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Rick O'Shay:
IS-A is a language independent concept. Consider a fictional language Latrine that creates classes dynamically at runtime:

Boose = factory.create( Banana, Moose );

The language says that a Boose may now be passed to any method that accepts a Banana, Moose or a Boose. In other words, a Boose IS-A Boose and a Banana and a Moose.

A implementing interface Juice in Java IS-A Juice. The IS-A relationship has nothing to do with the mechanics of inheritence per se.


Er... What?

You mean it's a concept common to all Object Oriented languages then?
[ July 03, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

The IS-A relationship has nothing to do with the mechanics of inheritence per se.

This assumes that the marketing literature and the subsequent texts portraying the doctrine hold true. Let's just make some assertions without any basis provided whatsoever, just like that literature does (after all, it has millions of followers):
- The only legitimate form of inheritance is interface inheritance.
- Concrete behaviour inheritance is an implicit software design flaw and requirement defect, assuming the requirements strve for maintainability, clarity, ease of use, and some other fundamentals that developers often implicitly strive for.
- In the context of Java, and the fact that concrete inheritance is illegitimate, this also implies that non-final classes, and the protected keyword should never be used. Don't be fooled into thinking that it is the extends keyword that shouldn't be used as Allen Holub has done http://www.jtiger.org/articles/why-extends-is-not-evil.html
- Given the only legitimate form of inheritance, the use of "is a" certainly describes inheritance - in fact, it would be silly to subtype an interface with another interface if those interfaces (aka contracts) don't have a "is a" relationship to its supertype contracts.
[ July 03, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1531
I am done with this thread, yes...

interface implementation is an IS-A relation.


atleast the "instanceOf" example at pages 13 & 14 in Chapter 3 from K&B should scream the same point. You have a class that implements an interface, now the objects of class succeed to be "instances of" the interface .
Yet to arrive chapter 5. I shall see what grinds up there. :roll:
[ July 07, 2005: Message edited by: Akhil Trivedi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing an interface - Is it IS-A ?