|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn this short tutorial we are going to discuss nested classes. Nested classes are classes within classes. Let us try and understand better by writing simple programs. P1.csclass OurOuterClass
{
public static void Main()
{
System.Console.WriteLine("OurOuterClass");
}
}
OutputOurOuterClass
The above program compiles and runs successfully to give the desired output. The above program consists of one single class named P2.csclass OurOuterClass
{
public static void Main()
{
System.Console.WriteLine("OurOuterClass");
}
class OurNestedClass
{
}
}
OutputOurOuterClass
The above program compiles and runs successfully to give the desired output. The above program consists of one single class named Let's understand how a nested class looks through one more example. P3.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("DemoClass");
}
}
class OuterClass
{
class NestedClass
{
}
}
OutputDemoClass
The above program compiles and runs successfully to give the desired output. The above program consists of three classes: the Now let's explore the nested classes. Can we create an object of P4.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("DemoClass");
NestedClass obj = new NestedClass();
}
}
class OuterClass
{
class NestedClass
{
}
}
OutputP4.cs(6,7): error CS0246: The type or namespace name 'NestedClass' could not
be found (are you missing a using directive or an assembly reference?)
P4.cs(6,29): error CS0246: The type or namespace name 'NestedClass' could not
be found (are you missing a using directive or an assembly reference?)
Error! That's because the P5.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("DemoClass");
}
}
class OuterClass
{
class NestedClass
{
}
public void CreateObjectOfNestedClass()
{
NestedClass obj = new NestedClass();
}
}
OutputDemoClass
The above program compiles and runs successfully to give the desired output. The above program shows that we can create an object of What if we want to get a reference to a P6.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("DemoClass");
OuterClass oc = new OuterClass();
NestedClass reference = oc.CreateObjectOfNestedClass();
}
}
class OuterClass
{
class NestedClass
{
}
public void CreateObjectOfNestedClass()
{
NestedClass obj = new NestedClass();
return obj;
}
}
OutputP6.cs(9,7): error CS0246: The type or namespace name 'NestedClass' could not
be found (are you missing a using directive or an assembly reference?)
Error! We did everything according to our initial plan. Then where did we fail? Well we failed at line 9 where we are referencing the nested class directly from P7.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("DemoClass");
OuterClass oc = new OuterClass();
OuterClass.NestedClass reference = oc.CreateObjectOfNestedClass();
}
}
class OuterClass
{
class NestedClass
{
}
public NestedClass CreateObjectOfNestedClass()
{
NestedClass obj = new NestedClass();
return obj;
}
}
OutputP7.cs(19,23): error CS0050: Inconsistent accessibility: return type
'OuterClass.NestedClass' is less accessible than method
'OuterClass.CreateObjectOfNestedClass()'
P7.cs(15,10): (Location of symbol related to previous error)
Error again! Now where did we go wrong? The compiler error tells us that we need to make the class P8.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("DemoClass");
OuterClass oc = new OuterClass();
OuterClass.NestedClass reference = oc.CreateObjectOfNestedClass();
}
}
class OuterClass
{
public class NestedClass
{
}
public NestedClass CreateObjectOfNestedClass()
{
NestedClass obj = new NestedClass();
return obj;
}
}
OutputDemoClass
Bingo! The above program compiles and runs successfully to give the desired output. We know that we can reference the P9.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("DemoClass");
OuterClass.NestedClass reference = new OuterClass.NestedClass();
}
}
class OuterClass
{
public class NestedClass
{
}
}
OutputDemoClass
The above program compiles and runs successfully to give the desired output. Wasn't that simple? Nested Classes and Static MembersLet us see how nested classes behave with the static members. P10.csclass Demo
{
public static void Main()
{
System.Console.WriteLine(OuterClass.NestedClass.x);
}
}
class OuterClass
{
public class NestedClass
{
public static int x = 100;
}
}
Output100
The instance class is not required to access static variables. The rules that apply to standalone classes in case of static variables also apply to nested classes. For nested classes, fully qualified names of nested classes must be used to access the static variables as shown by the above program. Nested Classes and Members in ContainersLet us see how nested classes behave with the members in containing classes. P11.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Demo");
}
}
class OuterClass
{
public int y = 100;
public class NestedClass
{
public void abc()
{
System.Console.WriteLine(y);
}
}
}
OutputP11.cs(17,35): error CS0038: Cannot access a nonstatic member of outer type
'OuterClass' via nested type 'OuterClass.NestedClass'
Error! That's because the P12.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Demo");
OuterClass.NestedClass.abc();
}
}
class OuterClass
{
public static int y = 100;
public class NestedClass
{
public static void abc()
{
System.Console.WriteLine(OuterClass.y);
}
}
}
OutputDemo
100
The above program compiles and runs successfully to give the desired output. It is not always necessary to declare an outer class member as static to make it directly available in the nested class. There is one more way by which the nested class can directly access non-static members of the container class. This is how: P13.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Demo");
OuterClass.NestedClass.abc();
}
}
class OuterClass
{
public int y = 100;
public class NestedClass
{
public static void abc()
{
OuterClass oc = new OuterClass();
System.Console.WriteLine(oc.y);
}
}
}
OutputDemo
100
The above program compiles and runs successfully to give the desired output. The Constructors and Nested ClassesLet us try to understand (through small examples) how constructors in outer and nested classes behave. P14.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Demo");
OuterClass oc = new OuterClass();
}
}
class OuterClass
{
public OuterClass()
{
System.Console.WriteLine("OuterClass");
}
public class NestedClass
{
public NestedClass()
{
System.Console.WriteLine("NestedClass");
}
}
}
OutputDemo
OuterClass
The above program compiles and runs successfully to give the desired output. An attempt was made to create an object of P15.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Demo");
OuterClass.NestedClass nc = new OuterClass.NestedClass();
}
}
class OuterClass
{
public OuterClass()
{
System.Console.WriteLine("OuterClass");
}
public class NestedClass
{
public NestedClass()
{
System.Console.WriteLine("NestedClass");
}
}
}
OutputDemo
NestedClass
The above program compiles and runs successfully to give the desired output. An attempt was made to create an object of Inheritance and Nested ClassesWe can inherit classes that have already inherited classes. This is how: P16.csclass OuterClass
{
public OuterClass()
{
System.Console.WriteLine("OuterClass");
}
public class NestedClass
{
public NestedClass()
{
System.Console.WriteLine("NestedClass");
}
}
}
class Demo : OuterClass
{
public static void Main()
{
Demo dc = new Demo();
System.Console.WriteLine("Demo");
}
}
OutputOuterClass
Demo
The above program compiles and runs successfully to give the desired output. The base class constructor gets executed first and then the derived class's constructor gets executed. A final question: can we have a P17.csclass Demo
{
public static void Main()
{
System.Console.WriteLine("Demo");
OuterClass.NestedClass.abc();
}
}
class OuterClass
{
private int y = 100;
public class NestedClass : OuterClass
{
public static void abc()
{
OuterClass oc = new OuterClass();
System.Console.WriteLine(oc.y);
}
}
}
OutputDemo
100
The above program compiles and runs successfully to give the desired output. It is perfectly fine to have a nested class that is derived from the outer class.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||