Only the Is-a relation is related to inheritance. The OO concept of the Is-a relationship is called specialization. Most OO languages use classes to implement the concept of types and inheritance as only implementation of specialization. In an Is-a relationship A Is-a B if A is a specialization of B. This means that every instance/object of type A can do at least the things that B can do, i.e. it shows the same behavior for the same message (messages are usually implemented as methods). In the case of C++ the previous example for inheritance looks like this:
class B {
};
class A : public B {
};
The Has-a relationship is not called inheritance but composition. As the name suggests A has a member variable of type B. A is not a specialization of B and thus does not inherit anything from B. The corresponding example in C++ would look like this:
class B {
};
class A {
B b;
};
As you can see class A does not have to show the same behavior as B. This means that A does not have to respond to the same messages as B (speaking of the concept), i.e. A does not have to have the same methods as B (speaking of the implementation in C++). They can be completely different or partially overlapping.
In the first example where A Is-a B this means that everywhere where your program expects an object of type B you can also provide an object of type A as it is also an object of type B. In the second example this is not the case. However, you cannot use an object of type B where an object of type A is expected.
Please note that I distinguish between the concept and the implementation of the concept in a particular programming language. The concepts I mentioned are specialization, composition, and message. In C++ specialization is implemented via inheritance and messages are implemented via methods. Objective-C, for example, really uses message (encoded as text strings) instead of method calls. And there are several object oriented languages that don't have classes or inheritance.
Only the Is-a relation is related to inheritance. The OO concept of the Is-a relationship is called specialization. Most OO languages use classes to implement the concept of types and inheritance as only implementation of specialization. In an Is-a relationship A Is-a B if A is a specialization of B. This means that every instance/object of type A can do at least the things that B can do, i.e. it shows the same behavior for the same message (messages are usually implemented as methods). In the case of C++ the previous example for inheritance looks like this:
class B {
};
class A : public B {
};
The Has-a relationship is not called inheritance but composition. As the name suggests A has a member variable of type B. A is not a specialization of B and thus does not inherit anything from B. The corresponding example in C++ would look like this:
class B {
};
class A {
B b;
};
As you can see class A does not have to show the same behavior as B. This means that A does not have to respond to the same messages as B (speaking of the concept), i.e. A does not have to have the same methods as B (speaking of the implementation in C++). They can be completely different or partially overlapping.
In the first example where A Is-a B this means that everywhere where your program expects an object of type B you can also provide an object of type A as it is also an object of type B. In the second example this is not the case. However, you cannot use an object of type B where an object of type A is expected.
Please note that I distinguish between the concept and the implementation of the concept in a particular programming language. The concepts I mentioned are specialization, composition, and message. In C++ specialization is implemented via inheritance and messages are implemented via methods. Objective-C, for example, really uses message (encoded as text strings) instead of method calls. And there are several object oriented languages that don't have classes or inheritance.