which one is better depends a lot on your requirements and a concrete example. Like so often there's not only one best solution which fits every problem.
But in general I'd say the "has-a" relationship, i.e. composition, is to prefer because it's easier to make changes to a system with composition later. If you have an inheritance hierarchy (the "is-a" relationship) things get more complicated if you have to make bigger changes to your existing classes. In particular if you have a deep inheritance tree things can even get worse.
Anyway, as I said it depends on the situation. Of course there are also many cases where inheritance is the right solution and it's not always easy to decide which one to use even if you get more experienced.
Without knowing any more details, I would say in your case you should use 'has-a', by which I mean class B would have an instance of class A in it and you could use it.
'is-a' should only be used if class B is really is a type of class A. Example 'is-a' would be used if you have a Person class, and then you want to create an Employee class, then I would make Employee as a sub-class of Person since an Employee 'is-a' Person.
In most situations HAS-A relationship is recommended over IS-A. You need IS-A relation if you think Class B is just an extension or more specialized form of Class A with more attributes or specific implementations.
Design rule: "Composition is preferred over Inheritance"
So for example: We have following classes
Ram Chhabra wrote:Thanks a lot to all. I got my answer , that composition is good over inheritance.
Thanks to all again.
Composition is preferred over inheritance when it makes sense. Someone used a car analogy, so I will go with that.
Let's look at an engine. It has many parts (pistons, belts, oxygen sensors, etc.). Now we wouldn't say that an engine IS-A piston, but rather an engine HAS-A piston.
On the same token, there are many different kinds of engines (gas, diesel, V-8, V-12). We wouldn't say an engine HAS-A V-12, or an engine HAS-A gas. We would say a V-12 IS-A(n) engine, or the gas type IS-A(n) engine.
I had to add the (n) to make it an. Sometimes I am a grammar freak.
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."