Hi all, I found this is-a question in my Learn Key mock exam that i don't agree with the answer. The question and the answer are bellow:
A) is-a relationships always rely on inheritance. B) is-a relationships always rely on instance variables. C) is-a relationships always require at least two class types. D) is-a relationships always rely on polymorphism. E) is-a relationships are always tighly coupled.
Answers with explanations: A, C and D are correct. B is incorrect. Inheritance is independent of the existence of instance variables. E is incorrect. Coupling is not used to evaluate classes within the same inheritance tree.
What i think from the answer: A) A cannot be true because an is-a relationship also applies when a class implements an interface. Example: java.lang.String implements Comparable, then, String is-a Comparable! there is no inheritance! C) i don't understand why we can't say that a String is-a String! Isn't it an is-a relationship? D) i can't get why does is-a always rely on polymorphism, i think that's because of my doubt on letter C.
A. is correct, because when you implement an interface you inherit all its methods and constants the interface may have. think of interfaces as a 100% abstract classes. (interfaces are the way that Java implements multiple inheritance).
C. the "is-a" refers to a relation so an object can NOT inherit it self. it may contain a reference to a reference of its type which is a "has-a" relationship but it can NEVER inherit itself.
D. Polymorphism is a Greek word and means "many forms" or "many shapes", regarding java that means that an object of type B that extends type A may be referred by references of type B of course BUT also by references of type A which means it has "many forms". So when when we speak for inheritance we also speak for polymorphism.