-1

What is the best way to share common properties in multiple classes ?

For example:

Class A
{
int a;
int b;
int c;
}

Class B
{
int a;
int b;
int d;
}

Since properties a and b in this example are getting duplicated, what would be the best way to share these properties ?

The purpose is to reuse the code as much as possible. Is it the right practice to use inheritance for just sharing data members ?

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    We need more context than this. – Joel Jan 14 '20 at 13:45
  • 1
    Define share. What are you trying to achieve? – Johnathan Barclay Jan 14 '20 at 13:46
  • 3
    you can use `Inheritance` . Still this is not sharing. You just do not have to rewrite a,b,c in A and B class. – panoskarajohn Jan 14 '20 at 13:48
  • 4
    The obvious options are inheritance or composition. Contextually neither is more appropriate than the other in such a contrived example, so pick whatever you like. – David Jan 14 '20 at 13:49
  • @panoskarajohn thanks. Is it the right practice to use inheritance for just sharing data members ? – Shailendra Saxena Jan 14 '20 at 13:49
  • 1
    Interfaces, if both classes where related you could use inheritance... But as is there is not enough information to provide a concrete answer – Cleptus Jan 14 '20 at 13:50
  • 1
    [Inheritance](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/inheritance). read this as starter – Mong Zhu Jan 14 '20 at 13:50
  • @ShailendraSaxena as many noted it is not clear what you want to do. Please do some reading before you ask here. – panoskarajohn Jan 14 '20 at 13:52
  • @panoskarajohn the purpose is to reuse the code as much as possible. – Shailendra Saxena Jan 14 '20 at 13:54
  • Well, with this little pseudeo-code without any meaningful names it´s really hard to give you an adequate answer. I suppose instead of having the properties in both classes, just out it in one class and reference that class within your second one. – MakePeaceGreatAgain Jan 14 '20 at 13:56
  • @ShailendraSaxena The whole OOP pillar is build on code reusability. SO is for more specific QA problems. You already got two very clear answers inside comments `Composition` and `Inheritance`. If you ask me though, why would need two classes with the same properties in the first place? So my first tip would be just delete class B and reuse class A. – panoskarajohn Jan 14 '20 at 14:00
  • 2
    @ShailendraSaxena: *"the purpose is to reuse the code as much as possible"* - That purpose is misguided. "As much as possible" puts the emphasis more on reducing the number of duplicated keystrokes and less on the actual meaning and supportability of the code. Re-use the code that makes sense to re-use. Just because two objects happen to have similar members doesn't necessarily mean those objects are related in any meaningful way. Again, the example you've given is *far too contrived* to provide a concrete answer. – David Jan 14 '20 at 14:08
  • @bradbury9: I'm surprised that no one else mentioned interfaces - it's the obvious choice where no behavior is involved. – Dave Doknjas Jan 14 '20 at 15:15

1 Answers1

5

The usual way to share properties and behaviors (methods/operations) between classes is to use inheritance:

Class X {    // Common base
   int a; 
   int b;
}
Class A:X {  // One specialization 
   int c; 
}
Class B:X {  // Another specialization
   int d; 
}

This facilitates reuse, since you may reuse the implementation of X in different contexts.

But using inheritance is not just about sharing some elements: inheritance also defines a genaralization/specialization relation: A is a special kind of X. This implies that one could hope that where an X is used, an A could be used as well. Moreover, this is a definitive relationship: an A is ALWAYS an X.

But as you pointed out, if it's just for sharing, and in absence of any conceptual relationship, it may not be the wisest idea to use inheritance to save a few keystrokes. In case of doubt you could prefer composition over inheritance:

Class X {    // Some elements to be shared
   int a; 
   int b;
}
Class A  {  // Independent class  
   X x; 
   int c; 
}
Class B:X {  // Another independent class
   X x; 
   int d; 
}

Each A and B would have a property x, that contains the shared a and b. The advantage is that A and B are not constraint to impelment X's interface. Furthermore the relation is dynamic (you could for example have no x defined, or the x can change). Furthermore, and depending on the language, you may even share the same x object between several A and B objects.

Christophe
  • 68,716
  • 7
  • 72
  • 138