Is it good to subclass a class only to separate some functional parts?

Posted by prostynick on Stack Overflow See other posts from Stack Overflow or by prostynick
Published on 2010-03-25T08:35:47Z Indexed on 2010/03/25 8:43 UTC
Read the original article Hit count: 287

Suppose we have abstract class A (all examples in C#)

public abstract class A
{
    private Foo foo;

    public A() { }

    public void DoSomethingUsingFoo()
    {
        //stuff
    }

    public void DoSomethingElseUsingFoo()
    {
        //stuff
    }

    //a lot of other stuff...
}

But we are able to split it into two classes A and B:

public abstract class A
{
    public A() { }

    //a lot of stuff...
}

public abstract class B : A
{
    private Foo foo;

    public B() : base() { }

    public void DoSomethingUsingFoo()
    {
        //stuff
    }

    public void DoSomethingElseUsingFoo()
    {
        //stuff
    }

    //nothing else or just some overrides of A stuff
}

That's good, but we are 99.99% sure, that no one will ever subclass A, because functionality in B is very important.

Is it still good to have two separate classes only to split some code into two parts and to separate functional elements?

© Stack Overflow or respective owner

Related posts about object-oriented-design

Related posts about object-oriented