java IS-A relationship exam question confusion
Asked Answered
A

6

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?

Araiza answered 13/7, 2010 at 13:38 Comment(7)
Does the context in which this appears specify a relationship among classes?Fiver
What I've entered is the whole question, start to finish.Araiza
@ck - thanks for that helpful contributionAraiza
Check out dlugosz.com/Perl6/web/isa-inheritance.htmlMisti
I don't like answer C - just because is-a can be modelled with interfaces too - it looks, this custom definition of class includes Java classes and interfacesIverson
@dairemac - sorry, I think that you may be right on a technicality, but it looks like (for once) the answer is biased towards actual use rather than technicalities. It also depends on how it was taught in your syllabus, not on what is actually right...Coheman
@Andreas: That's ultimately a language feature (well, ok, UML has it too; but even then the arrow is still roughly the same). Nothing prevents you from treating interfaces as the ultimate abstract class.Narthex
N
1

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.

Narthex answered 13/7, 2010 at 13:44 Comment(4)
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".Araiza
@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.Narthex
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.Araiza
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.Narthex
R
4

"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.

Risser answered 13/7, 2010 at 13:44 Comment(2)
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?Araiza
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.Araiza
W
3

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.

Walkyrie answered 13/7, 2010 at 13:49 Comment(4)
Zen Buddhists are more interested in an object not being itself :-)Ianiana
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.Araiza
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.Walkyrie
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...Araiza
N
1

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.

Narthex answered 13/7, 2010 at 13:44 Comment(4)
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".Araiza
@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.Narthex
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.Araiza
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.Narthex
I
1

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.

Iverson answered 13/7, 2010 at 13:44 Comment(3)
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.Araiza
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.Iverson
"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!Araiza
I
1

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.

Innocuous answered 13/7, 2010 at 15:23 Comment(0)
M
0

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

Midlands answered 13/7, 2010 at 13:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.