views:

413

answers:

6

Hi all,

I have something like this:

classes A to D all have variables var1, var2, var3, var4, var5
class A has an extra variable var6
class B doesn't have var6, but do have the variables var7, var8
class C is the same as B, but has an extra variable var9
class D only has an extra variable, var10

How should i implement this?

The behavior of all the classes is the same: show data. (In a later stage all the classes need save, update and delete functionality. The name of the methods are the same, but the implementation is different.)

+2  A: 

I think you should take a look into the inheritance concept

Sergio
+2  A: 

Inherit A from X. (X having 1-5) and add 6 to A, X being and abstract class. Inherit D from X. and add 10, Inherit B from X and add 7 and 8 and so on and so forth.

Hope you get the concept.

Drejc
but this ain't very easy-reference, right?
Martijn
+1 for obsfucated answer to obsfucated question!
George Stocker
Reference should be through properties or getter/setter methods.
Gamecat
Life ain't easy ... but thats how it is ;)
Drejc
+1  A: 

class Parent { public var var1, var2, var3, var4, var5; }

class A : Parent { public var var6; }

class B : Parent { public var var7, var8; }

class C : B { public var var9; }

class D : Parent { pubilc var var10; }

ng5000
Are you sure you want to make everything public?
George Stocker
I wasn't paying any attention to scope really, just trying to be as basic as possible for the original question asker.
ng5000
+11  A: 
Nathan W
Thnx! Looks nice!
Martijn
A: 

Here and Here for good tutorials on inheritance.

xan
+8  A: 

Another reason to enter some monday afternoon ascii art:

           *-------*
           |class X|
           |-------|
           | var1-6|
           *-------*
               ^
               |
    ________________________
    |          |           |
*-------*  *-------*   *-------*
|class A|  |class B|   |class D|
*-------*  *-------*   *-------*
| var6  |  | var7,8|   | var10 |
*-------*  *-------*   *-------*
               ^
               |
               |
           *-------*
           |class C|
           |-------|
           | var9  |
           *-------*
Gamecat
did you just type that out?
Nathan W
Yup, its monday afternoon.
Gamecat
Wow man +1 using ASCII art
JoshBerke