Submitted by c-admin on Wed, 05/29/2019 - 00:27
Question:
Consider this code:
interface X1{ }
interface X2{ }
class A { }
class B extends A implements X1{ }
class C extends B implements X2{
D d = new D();
}
class D { } Which of the following statements are true?
Select 3 options
A. D is-a B.
B. B has-a D.
C. C is-a A
D. C is-a X1
E. C is-a X2
Answer and Explanation (Click to expand) Answer:
C - Because C 'is-a' B and B 'is-a' A.
D - Because C is-a B and B is-a X1.
E - Because C implements X2
Explanation:
Consider this code:
class A extends B implements I{
D d = new D(); }
Now, Inheritance defines an is-a relation , so A is-a B because A extends B. This actually means that A can be used in all the places where B is used. A can substitute for B anywhere because A is-a B. As all objects have Object as their super class, every object 'is-a' Object.
Since A implements I, it is sometimes said that A 'is-like-a' I. That is, although A is not a I but for all purposes A is like an I. The distinction between is-a and is-like-a is not important for the exam. For the purpose of the exam, is-like-a is same as is-a.
Aggregation defines a has-a relation. Here, D is a member object in A so D is contained in A. So it is said that A 'has-a' D.
All Java objects have the class Object as the ultimate superclass, and so Object is always at the root of any inheritance hierarchy.