none
Instance of Nested Sealed class.. RRS feed

  • General discussion

  • Hello Fellas, 

    I have one small question. Please, see the next code

    class A
    {
       public static void MethodX_Static(){...}
       public void MethodX_Dynamic(){...}
    
       public sealed class NestedBaby : SomeBaby
       {
         public override Baby GetBaby();
       }
    }
    
    abstract class SomeBaby
    {
      public abstract Baby GetBaby(){...}
    }
    
    // Somewhere in code
    A a = new A();

    Please, tell me - now using pointer a I will get new instance of MethodX_Dynamic, but what about the NestedBaby

    Will it be also reinstantiated with the new overrided abstract GetBaby method? Or NestedBaby will be still the same like static?

    Thanks


    "I = I + 1" .. Isn't it boolshit?



    • Edited by RobbKirk Wednesday, September 25, 2019 12:55 AM
    Wednesday, September 25, 2019 12:37 AM

All replies

  • Greetings Robb.

    Does this answer your question?

    namespace ConsoleApp
    {
       class Program
       {
          static void Main(string[] args)
          {
             A a1 = new A("Fred");
             A a2 = new A("Sally");
    
             a1.GetBaby();
             a2.GetBaby();
          }
       }
    
       class A
       {
          public static void MethodX_Static() { }
          public void MethodX_Dynamic() { }
    
          NestedBaby NB { get; set; }
    
          public A(string name)
          {
             NB = new NestedBaby(name);
          }
    
          public Baby GetBaby()
          {
             return NB.GetBaby();
          }
    
    
          sealed class NestedBaby : SomeBaby
          {
             Baby MyBaby;
    
             public NestedBaby(string name)
             {
                MyBaby = new Baby(name);
             }
    
             public override Baby GetBaby()
             {
                Console.WriteLine("My baby is called " + MyBaby.Name);
                return MyBaby;
             }
          }
       }
       abstract class SomeBaby
       {
          public abstract Baby GetBaby();
       }
    
       public class Baby
       {
          public string Name { get; set; }
          public Baby(string name)
          {
             Name = name;
          }
       }
    }

    Output is;

    Wednesday, September 25, 2019 1:16 AM