KyaPoocha.com

Huge Collection of Interview Questions


What is a nested class? Why can it be useful? 

A nested class is a class enclosed within the scope of another class. For example:

// Example 1: Nested class
//
class OuterClass
{
class NestedClass
{
// …
};
// …
};

Nested classes are useful for organizing code and controlling access and dependencies. Nested classes obey access rules just like other parts of a class do; so, in Example 1, if Nested Class is public then any code can name it as OuterClass::NestedClass. Often nested classes contain private implementation details, and are therefore made private; in Example 1, if Nested Class is private, then only OuterClass’s members and friends can use Nested Class. When you instantiate as outer class, it won’t instantiate inside class.



If you liked this question/asnwer, make sure you
Subscribe to KyaPoocha.com RSS feed!


BOOKMARK THIS : BlinkList | del.icio.us | Digg it | Furl | reddit | StumbleUpon | Yahoo MyWeb |