Why are nested classes "inherited"?
Asked Answered
C

2

6

Consider the following code example:

class Outer
{
    public class Nested { }
}

class SubOuter : Outer { }

class Test
{
    Test()
    {
        Outer.Nested x;     // Makes sense.
        SubOuter.Nested y;  // Compiles, but why?
    }
}

It appears that the nested class is "inherited" by the subclass. Where's the point in that? What can I do with this feature that I cannot (or cannot easily) do otherwise? Aren't Outer.Nested and SubOuter.Nested exactly equivalent?

Clarification: Of course it compiles because the C# spec says so. I understand that. I am asking why C# was designed that way, since it does not seem to add something to the language. If you have an example to the contrary, i.e., some code that gets easier/shorter/better by using this feature, please share it in an answer and I will gladly accept it.

Casseycassi answered 13/1, 2016 at 8:5 Comment(9)
because the Nested is public, so the SubOuter class can see it in the base class.Clyve
Where is the problem, why you do not want this to compile.Clyve
Outer.Nested and SubOuter.Nested is equivalent, there is no difference in them except the way of declaration.Clyve
Features have costs. Thus, features are only added to a language if they add some benefit to the language. I don't understand the benefit of this feature. I'd like to understand it, so that I can use it wisely. Thus, if you have an example where this feature can make code shorter/easier to read/etc. than it would be otherwise, please share it.Casseycassi
No, you're asking for a new feature. All public members of a base class are accessible through inheriting classes. Why should classes be treated differently then e.g. methods, and why should the compiler team have to specifically add code to prevent you from making such accesses? (And then get the error message translated into various languages, document it, etc)Glazunov
@Damien_The_Unbeliever: So basically you are saying "Because inheriting non-instance members such as static methods and nested classes was easier implementation-wise than not inheriting them". Is that right? If yes, that would make a fine answer.Casseycassi
It's more close to "not inheriting public non-instance members of a class would completely go against what people expect.". It's what I would expect at least. Apart from that, it would probably also have a considerable implementation cost.Coates
@IanMercer: The LSP applies to object instances. Outer and SubOuter are class names, not object instances.Casseycassi
Of course if types are not heritable members then code inside SubOuter using Nested would probably be more cumbersome (assuming you maintain consistency across access to different kinds of members). You could no longer use the simple name Nested, it would always have to be referred to as Outer.Nested.Upas
N
6

From the perspective of Test Nested is just an identifier as if it were a member e.g. As it is public You may access it everywhere where you cann access any of the classes Outer or SubOuter. However both usages are identical, they identify the same class.

You may even reference the one by the other:

Outer.Nested x = new Outer.Nested(); 
SubOuter.Nested y = x;

Or even

Outer.Nested x = new SubOuter.Nested();
SubOuter.Nested y = x;

However as you may see in the debugger both x and y share a reference to Outer.Nested instead of SubOuter.Nested.

EDIT: As other already mentioned this is no new feature, it is simply the usual treating of members within classes. Thus it surely does not add any benefit to the language as the feature as you describe it simply does not exist. This therefor follows the Principle of least astonishment.

Treating nested classes differenetly however would be a new feature.

Neaten answered 13/1, 2016 at 8:11 Comment(0)
J
3

Because the Nested class is public, so the SubOuter class can see it in the base class.

And you are correct. Outer.Nested and SubOuter.Nested are equivalent, there is no difference in them except the way of declaration.

Update

according to your comment that the features have costs. this feature is already implemented in the .NET (I mean subclass can see the public and protected members of the base class) so there is no additional cost to implement this is .NET.

you can consider the Nested class like any other property in the base class.

this is very important explain which written in the comments on your question by @Damien_The_Unbeliever.

No, you're asking for a new feature. All public members of a base class are accessible through inheriting classes. Why should classes be treated differently than e.g. methods, and why should the compiler team have to specifically add code to prevent you from making such accesses? (And then get the error message translated into various languages, document it, etc)

Jinny answered 13/1, 2016 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.