2

I have two classes class A and class B. I want class B to have functionality of class A plus some more functionality of its own. One of the very simple ways to do this is to inherit class B from class A. But I cannot do this for some implementation specific reasons.

I have thought of another solution where in I will create another class class C which will contain all the virtual functions. The classes A and B will inherit from class C. The class A will contain the implementation of virtual functions and class B can also define the same set of functions and hence class B will have functionality of class A. Is this the optimal solution? Are there any better solutions than this other than direct inheriting class B from class A?

ajay bidari
  • 693
  • 2
  • 10
  • 22
  • 5
    *"But I cannot do this for some implementation specific reasons"* is not a good but often (ab)used excuse for bad designs. If your design demands Inheritance use it and if some implementation prevents you from doing so, change it. – Alok Save Oct 29 '12 at 09:33
  • What are the "implementation specific reasons"? – Andrzej Oct 29 '12 at 09:35
  • If you have control over what both A and B inherit from (and you must by the proposal of you C-->A, C-->B solution) I fail to see why you suddenly *lack* that control to simply let A-->B and be done with it. – WhozCraig Oct 29 '12 at 09:49

4 Answers4

11

What you are looking for is called Composition (over Inheritance).

Composition over inheritance (or Composite Reuse Principle) in object-oriented programming is a technique by which classes may achieve polymorphic behavior and code reuse by containing other classes that implement the desired functionality instead of through inheritance.

This means that instead of having class B inherit from class A, you can have class B contain an instance of class A. This is also good coding practice to avoid tight coupling between class A and class B.

alestanis
  • 21,519
  • 4
  • 48
  • 67
0

You can do that however if you create class C to be an abstract base class and have class A and class B inheriting from it, then you realize that class A and class B would be unrelated except for the fact that they inherit from the same abstract base class. That means that you would not be able to cast instances of class B to A, etc. In general such implementation depends on the use cases.

E.g.:

Your class C is Person, class A is Teacher and class B is Student. Then a student is not a teacher and a teacher is not a student, so such implementation is valid.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
  • If class C is not an abstract class and if there is some default functionality provided in class C then both class A and class B will share the functionality of class C. – ajay bidari Oct 29 '12 at 09:55
0

The class A will contain the implementation of virtual functions and class B can also define the same set of functions

This way you'll have code-duplication and it's not a good thing.

If you don't want to use inheritance, try composition.

I explained composition earlier here.

Community
  • 1
  • 1
Azodious
  • 13,752
  • 1
  • 36
  • 71
0

You can use proxy classes, by creating two other classes PA and PB where PB inherits from PA. These proxy classes duplicate the interfaces of A and B respectively, but don't actually implement them. Instead, they just call the functions in an instance of A and B.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621