0

I have a bunch of derived classes that inherit from a base class. Each derived class still has unique members. One of these derived classes needs to be able to access all the other derived class's members. My aim with this problem is to write as little code as possible.

Would it be sloppy or bad practice to initialize all the derived classes' members in the base class so that one of the derived classes can access those members, instead of initializing them in said derived class?

Proposed approach:

public class BaseClass {
    public BaseClass() {
        ...
        der1Initializer = Der1Initializer(new Factory1());
        der2Initializer = Der1Initializer(new Factory2());

        List initializers = new List(){ der1Initializer , der2Initializer };

        der3Initializer = Der3Initializer(initializers);
    }

    Der1Initializer der1Initializer;
    Der2Initializer der2Initializer;
    Der3Initializer der3Initializer;
}

public class DerivedClass1 : BaseClass {
    public SomeFunction {
        der1Initializer.init();
    }
}

public class DerivedClass2 : BaseClass {
    public SomeFunction {
        der2Initializer.init();
    }
}

public class DerivedClass3 : BaseClass {
    ...
}

So that:

public class Der3Initializer {
    public GroupInitializationFunction {
        initializers[0].init();    //der1Initializer
        initializers[1].init();    //der2Initializer
    }
}

Instead of:

public class BaseClass {
    public BaseClass() {
        ...
}

public class DerivedClass1 : BaseClass {
    public DerivedClass1 {
        der1Initializer = Der1Initializer(new Factory1());
    }

    public SomeFunction {
        der1Initializer.init();
    }
    Der1Initializer der1Initializer;
}

public class DerivedClass2 : BaseClass {
    public DerivedClass2 {
        der2Initializer = Der2Initializer(new Factory2());
    }

    public SomeFunction {
        der2Initializer.init();
    }
    Der2Initializer der2Initializer;
}

public class DerivedClass3 : BaseClass {
    public DerivedClass3 {
        List initializers = new List()
        {    
            Der1Initializer(new Factory1()), 
            Der2Initializer(new Factory2());
        };

        der3Initializer = Der3Initializer(initializers);
    }
    ...
    Der3Initializer der3Initializer;
}
...
public class Der3Initializer {
    public GroupInitializationFunction {
        initializers[0].init();    //der1Initializer
        initializers[1].init();    //der2Initializer
    }
}

This is a vast oversimplification of the "problem". The purpose of this code is to reduce duplicate code and the need to reinitialize members that can be shared and to optimize performance.

I am aware that it isn't necessarily a good idea to give other classes access to members they aren't using.

I just thought this is an interesting problem regarding code separation vs. duplicate code.

  • This sounds like a terrible idea. If you want to share data between classes in this way, put that data into a separate class and pass an instance of that class to the constructors of the classes that need to use it. – Matthew Watson Apr 11 '19 at 08:14
  • @MatthewWatson, can you please expand on why it is a terrible idea? DerivedClass3 is already that separate class to contain the member variables, but I want to avoid reinitializing members. – Dévan Coetzee Apr 11 '19 at 08:21
  • 2
    Every time you add a new derived class you will need to modify the base class. This violates the ["Open/Closed" principle](https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle) - that's why it's a bad idea. – Matthew Watson Apr 11 '19 at 08:42
  • The rule is very simple: a base class should have no knowledge of it's derived classes. – Zohar Peled Apr 11 '19 at 08:55
  • @ZoharPeled, the base class isn't aware of the derived classes. It only initializes the shared member variables. – Dévan Coetzee Apr 11 '19 at 09:00

1 Answers1

2

I would not recommend initializing childs on the parent class as

  • Violates Open Closed Principle as the base class has the responsibility of initializing its children and there will always be the need to add new child classes and consequently modifying base class .

  • Violates Single Responsibility Principle as this is not the role for the base class (to initialize its childs )

  • Violates Inversion of Control as base class is tightly coupled to child class see this link

  • Thank you for your answer. I think the most accurate would be to say that it violates the **Open-Closed Principle**. The base class has responsibility for initializing the children, it only initializes the member variables of the child classes. – Dévan Coetzee Apr 12 '19 at 07:25
  • Thank you Devan , i have updated the answer to be more descriptive. – Mahmoud-Abdelslam Apr 12 '19 at 11:50