1

Just need some help with nested classes and inheritance..

namespace blah
{
    private abstract class InheritedSomething
    {
        public void doSomething() {};
    }

    class OtherClass : InheritedSomething
    {
        //Stuff

        class NestedClass : InheritedSomething
        {
            //Stuff
        }
    }
}

Is this the correct way of doing things? It seems a bit redundant that NestedClass inherits InerhitedSomething when its parent class already inherits this... But when I remove the inheritance from NestedClass it asks me to make doSomething() a static method.

So my question is, which way is "correct" - Both OtherClass and NestedClass inheriting InheritedSomething or only OtherClass inherits InheritedSomething and doSomething() becomes public static void?

Thanks!

Travv92
  • 791
  • 4
  • 14
  • 27
  • Should doSomething have the same implementation in both classes? Or does doSomething differ in OtherClass vs NestedClass? – Steven Wexler May 10 '13 at 04:53

2 Answers2

0

There's nothing wrong with this design.

The suggestion that a method can be made static is a completelly separate issue. Note, that can doesn't mean it should. (Read more about deciding here or here). When you remove the inheritance, doSomething isn't overriding anymore which makes it eligible to be checked for this suggestion.

Community
  • 1
  • 1
lisp
  • 4,138
  • 2
  • 26
  • 43
0

Correct or not depends on what you intend to do.

Inner types have privileged access to enclosing type members but do not derive from it.

What is your intention with these classes?

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59