Class inheriting itself

Hi All,

       I have a very serious doubt of whether a class can inherit itself. Though their is no purpose, i just want to know whether a class inherits itself. I have provided a sample code for you to check that it actually does which very much proves the concept of inheritance to be wrong.

 

public class Sample
 {
  
    
  public class Sample1:Sample
  {
      
   public  Sample1()
   {
    Console.WriteLine("Sample1 Constructor wihtout Argument");
   }
   
   public void a()
   {
    Console.WriteLine("Main2");
   }
   
  }
  
 }
 

 public class Demo : Sample.Sample1
 {
  new public void a()
  {
   Demo o = new Demo();
   test();
  }
  public static void Main()
  {
   Sample obj = new Sample();
   Sample.Sample1 objsample1 = new Sample.Sample1();
   Sample.sample1.Sample1.Sample1 o = new  Sample.sample1.Sample1.Sample1

Demo.Sample1.Sample1 o3 = new Sample.Sample1.Sample1();

 

 }
 }

 

In this code i can create as many instance as possible which proves that the Sample1 class is being inherited indirectly.

Can anyone give me a reasonable answer for this

 

Thanks,

Murali.P

 



Answer this question

Class inheriting itself

  • TechedRonan

    OK, I'll try my best to answer altough in my mind the answer is simply "Why not "

    1. This because of how the name lookup works:

    When you write

    A.B

    the compiler looks for the B name in the A class.

    When you inherit A from B, A contains the name of B and so B will also contain the name B, its own name.

    So if you write

    A.B.B

    the compiler looks for B in A, finds a class and now it looks again for B in this class. Of course, it finds it again but this is still the B class.

    Probably the compiler can probably avoid this by "ignoring" the name of the class itself when looking up a name in a class (because nested classes with the same name are not permitted anyway, at least in C#) but this is really a special case and it probably does not worth the effort to go around it. Perhaps it can be considered a bug, I cannot find anything about this in the C# spec.

    2. Since the usefulness of inheritance should be clear the question is more like "why making the B class (that inherits A) a nested class of A". Nested classes are usually used when

    • they should not be "seen" by other users of A (you can make B private or protected)
    • they are strongly related with the class that "nests" them
    • they need access to some members of A that are "protected".

    If I remember correctly I once wrote something like

    class Layout {

    private class VerticalLayout : Layout {
    }

    private class HorizontalLayout : Layout {
    }

    public static Layout GetLayout() {

    if (some_condition)

    return new VerticalLayout();

    else

    return new HorizontalLayout();

    }
    }

    Basically I did not want others to be concerned with existence of VerticalLayout or HorizontalLayout (or any other possibile layout) so I just made them private nested classes and provided a public static method to get an instance of the correct layout (based on some settings).


  • jambi

    To me, a class that inherits itself is like

    class Foo : Foo

    {
    }

    and this does not compile.

    What you have there is a nested class that inherits its parent class. This works fine and there is nothing wrong about it, it's even useful sometimes.

    What is a bit confusing is that the compiler allows you to write Sample.Sample1.Sample1...Sample1 altough it is really just Sample.Sample1. To simplify things:

    namespace ConsoleApplication1 {

    class A {

    public class B : A {

    }}

    class Program {

    static void Main(string[] args) {

    A.B.B.B.B.B b = new A.B.B.B(); // note that the number of B's in the left side does not equal the number of B's in the right part but it compiles. That's because A.B.B.B is really just A.B, there is no class A.B.B.B in reality. You can write this because inheriting from A makes the B class name accessible through itself, it does not create some other class

    Console.WriteLine(b.GetType().FullName); // To be even clear, this will print out ConsoleApplication1.A+B

    }}}

     


  • Can-Ann

    Hi Mike,

    Thanks for the explanation. Now its quite clear for me. But two points are still pestering me.

    1. Why the compiler should allow us to write Sample.Sample1.Sample1.Sample1.Sample1 o = new Sample.Sample1();

    2. Whats the real purpose and advantage of "nested class inheriting its parent class" as you have mentioned and "Inner classes".

    Thanks,

    Murali


  • Class inheriting itself