Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IS-A between class and interface

 
Jani Joeli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all!

I did the mock exam from LearnKey SCJP5 study guide CD (u know, the one that came with the excellent S&B book), and there was a question about IS-A relationships. One of the options was "IS-A relationships always require at least two class types", which was marked as correct.

I got a bit confused there: if a class implements an interface (ie. Ball implements Bounceable), then it can be said that "Ball IS-A Bounceable", right? In that case, is an interface considered to be a special kind of class to fulfil the "atleast two class types" rule?

Please correct me if I'm completely lost. Thanks for any help!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jani Joeli:
...if a class implements an interface (ie. Ball implements Bounceable), then it can be said that "Ball IS-A Bounceable", right? In that case, is an interface considered to be a special kind of class to fulfil the "atleast two class types" rule? ...

Welcome to JavaRanch!

An interface is not really a "special kind of class," but it is a type. So I might remove the word "class" from that rule to simply say, "at least two types."

On the other hand, maybe that's not quite right either. Isn't it accurate to say a Ball IS-A Ball? (Then again, if we're talking about "IS-A" in terms of polymorphism, then we wouldn't have much "poly" if there's only one type involved.)
 
maddy saddy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Jani IS-A relationships always require at least two class types.

class B
{
....
}
class A extends B
{
....
}
Now you see IS-A relationships always require at least two class types.
class A extends B means class A is of type class B or class A is class B.
IS-A relationship works only with inheritence(A extends B) and implementation(interfaces).

I hope this will clear your doubt.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by maddy saddy:
...IS-A relationship works only with inheritence(A extends B) and implementation(interfaces)...

But this is the original poster's point: An interface is not a class, so how can you say IS-A always requires two classes?
 
Tor Henning Post
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with marc. And look at the question formulation "class type"

If you have a class "Horse" that implements the interface "Animal"
Then the Horse IS A animal.
 
Jani Joeli
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:

But this is the original poster's point: An interface is not a class, so how can you say IS-A always requires two classes?


Conclusion: the option "IS-A relationships always require at least two class types" should be marked incorrect if similar question comes up in the real exam? I just want to make this clear to myself so when doing the exam, I won't screw up one of the easy ones..

Thanks very much for help!
 
Tor Henning Post
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the statement is correct. There is a difference between class and class types. When you implement an interface you get a IS-A relationship to the class type that the interface represents.

See my previous post about the horsi
 
Jeremia Morling
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify for future students (found the same in certfication exam for Java 6). I think what Tor was saying is that the key lies in the question:
"When considering relationships between classes, which statement(s) are true? (Choose all that apply.)"
You are asked to only look at relationships between classes, not between classes and interfaces or between types.
In that case it makes sense that "is-a relationships always rely on inheritance", because we are not talkting about interfaces.

Stupid formulation of question/answer really!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic