views:

163

answers:

6

From MasterExam:

Which statements are true? (Choose all that apply)

A. is-a relationship always rely on inheritance
B. is-a relationship always rely on instance variables
C. is-a relationship always require at least two class types
D. is-a relationship always rely on polymorphism
E. is-a relationship are always tightly coupled

Correct answers: A, C, D

I don't see how any of A, C or D are correct.

An Object object IS-A Object. A String object IS-A String. There is only one class type in each of these statements and no inheritance or polymorphism applies.

Is my rationale wrong or are the answers incorrect?

+4  A: 

"Relationship" refers to a relationship between two classes. An is-a relationship is a type of relationship that uses inheritance (as opposed to e.g. has-a, which uses composition). For instance, a String is-a Object. A class can't inherit from itself, which implies C. As a side note, a class could have composition (has-a) with itself. E.g. a Person could have another Person as a mother field.

Any time you have inheritance, an instance of the subclass can be used as an instance of the superclass. That's polymorphism, which means D is also right.

Matthew Flaschen
Maybe you're right, the important word here is "relationship" which suggests two different classes. I'm still not 100% convinced but I'm going to mark this as the accepted answer.We are therefore saying:A Dog IS-A Dog - true.A Dog IS-A Animal - true.A Dog has a IS-A relationship with Dog - false.A dog has a IS-A relationship with Animal - true.As an aside, my comments are appearing without the line-breaks I've entered, why is that?
dairemac
@dairemac - related to the comments line-break, that's just how it is...
ck
As stated below - I'm changing my understanding of IS-A. Previously I would have equated an IS-A relationship with the "instanceof" keyword i.e. if(d instance of Dog) then d's class type IS-A Dog. I am now saying that this is not necessarily the case. I will now equate IS-A with inheritance. Previously I would have said that "Dog IS-A Dog" was true but henceforth I will say that that is false. Thanks for all the feedback.
dairemac
+1  A: 

Your rationale is slightly off, as this relationship applies to classes, not to objects.

A string IS-A object since String inherits from Object. Similarly a FileOutputStream IS-A OutputStream IS-A Object.

IS-A is a relationship between classes, no between classes and objects.

Joey
Can you elaborate? I don't see the relevance of the distinction between classes and objects.A FileOutputStream IS-A OutputStream IS-A Object - inheritance.Also a FileOutputStream IS-A FileOutputStream - no inheritance.Answer A states: "is-a relationship ALWAYS rely on inheritance".
dairemac
@dairemac: An `IS-A` relationship is modeled between entities in an ER diagram for example – those relationships *always* become inheritance when moving to e.g. UML. You don't draw circles in that graph and declare everything a subclass of itself. Regarding Liskov's substitution principle it may make sense to treat it that way when programming but no class inherits from itself. Therefore »Foo IS-A Foo« is purely nonsensical. Also, the distinction between classes and objects should have been made pretty clear at the very beginning of your OOP classes.
Joey
Thanks for the response Johannes. You are equating IS-A with inheritance where I would have equated IS-A with anything that returns true using the instanceof keyword. Maybe you're right. I am, of course, well aware of the distinction between classes and objects - I was merely querying the relevance of the distinction to this query.
dairemac
At least according to what I have learned, IS-A is a relationship between entities or classes. It is solely used in modeling and never with actual objects.
Joey
+1  A: 

You need to see it a bit different:

  • A cat is-a animal
  • A car has-a engine
  • A String is-a object
  • A String has-a char array

Try to see it that way, it should become clearer now.

Andreas_D
Not convinced to be honest.1. A Cat is-a Animal.2. A Cat is-a Cat.Option A states "is-a relationship ALWAYS rely on inheritance". Inheritance applies in 1 but not in 2.
dairemac
For composition, this is posible: a Father **has-a** Father. But for inheritance (is-a), we only consider different types. **is-a** with same types is always true and maybe, in theorie, every type inherits itself.
Andreas_D
"in theorie, every type inherits itself" - incorrect in my opinion."is-a with same types is always true" - that's exactly what I'm saying!
dairemac
A: 

Sorry, your rational is wrong. They are talking about two different classes. For example a String is-a Object.

Derek Clarkson
+3  A: 

A. That a String is a String is... a tautology, considered obvious. But looking at the API documentation, you find that a java.lang.String is also a java.lang.Object. In fact, all Java classes inherit from java.lang.Object. They inherit basic properties of Object and add some others of their own. That's what the is-a relationship is all about.

C. Again, the tautology about any object being itself is of interest to Zen Buddhists but not language designers and other Computer Scientists. So to have an "is-a" relationship, you need two distinct classes.

D. I'm not so sure here. If I were asked, I'd say polymorphism depends on the "is-a" relationship, not the other way around.

Carl Smotricz
Zen Buddhists are more interested in an object _not_ being itself :-)
Péter Török
I had to look "tautology" up in the dictionary! One definition is "a statement that is ALWAYS TRUE". The exam question asks which answers are TRUE and the supplied answers all include the word ALWAYS.As a computer scientist, I use logic and logic dictates that if something is obviously true then it is still true!As stated in a different answer here, maybe the key word here is "relationship" implying two different classes but it's not 100% clear. Thanks for the feedback.
dairemac
Still, Computer Science is not Philosophy. That a cat is a cat is not relevant/interesting/significant from the standpoint of software. Or stated differently: Being right won't help you if your prof decides not to accept your answer.
Carl Smotricz
My previous understanding, in the context of computer science and the java programming language, was that the statement "java.lang.String class IS-A java.lang.String" was true and that the statement "java.lang.String class IS-A java.lang.Object" class was also true. Based on the answers here, I now consider the former to be false and only the latter to be true - no philosophising or Zen enlightenment required...
dairemac
+1  A: 

I think points A & C are pretty clear by now... regarding D.though ,it may be technically possible to extend a class with no instance behavior or property..(blank or maybe only with static elements) ...but that in principle kind of defeats the purpose of "extending" classes as you want to inherit behavior/functionality from classes higher up the inheritance tree. So,i would say option D. is correct(almost) :) (if we are not going too technical).btw this can be an interesting discussion.

Soumava