tags:

views:

64

answers:

2

Hi, I have previously asked this question and since I already accepted an answer, I thought I would ask another question separately.

http://stackoverflow.com/questions/3995673/recommended-program-structure

My query is: the different classes each have a lot of overlap but perhaps the odd boolean variable or string different - so I can store these in private variables in the classes, but the common methods (defined in the parent class) always use the parent values.

Now, I know this makes sense - for the parent to use it's own values. But is there a way to implement the system I want without just replicating the same methods in every child class? It would be nice to be able to use the parent class for this so that future changes need not be changed in 8 different places, as well as just being tidier.

I hope this makes sense and any feedback would be much appreciated!

+2  A: 

Maybe you should investigate using Composition rather than Inheritance. That's a whole different way of thinking in OOP, but it allows for much more flexibility.

Here is a nicely rated article from CodeProject on the subject : CP Article.

jv42
+1  A: 

Anything beyond standard inheritance?

public class YourBaseClass { // perhaps abstract
    public int SomeParentValue {get;set;}
    public void SomeParentMethod() {Console.WriteLine(SomeParentValue);}
}
public class A : YourBaseClass {
    public string SomeValueA {get;set;}
}
public class B : YourBaseClass {
    public DateTime SomeValueB {get;set;}
}
public class C : A {
    public bool SomeValueC {get;set;}
}
Marc Gravell