Classes And Objects - Nested Classes
A nested class is a class defined within another class. As a consequence of this, it requires an instance of the outer class to be instantiated. Nested classes can be useful for logically grouping classes. Here's an example:
Now, this is how we can create a Dog and its nested Collar objects:
Notice how we had to use the syntax myDog.new Collar() to create a new Collar instance, instead of just new Collar() alone.
Note that nested classes can also be static, which means they don't require an instance of the outer class to be instantiated. We will talk about the static keyword in Encapsulation.
ByteBrawl
0