Can it possible redefine class with subclass when inherit base class with sub class?

A

ABC

I want define a base class as:

class abc
{
class cde
{
}
}

when I inhert abc class,

class ijk : abc
{
class cde
{
int io = 5; // Added
int xy = 8; // Added
}
}

If it possible, how should I to write this class and inherit class? abstract
or virtual?
 
B

Bruce Wood

I may be wrong (and I'm sure that everyone here will jump all over me
if I am), but in C# declaring a class within another class does _not_
make the inner class an inheritable part of the outer class.

In other words, in your example, abc.cde and ijk.cde are two completely
different classes that have nothing to do with each other. ijk.cde is
_not_ a "subclass" of abc.cde, unless you declare it like this:

class ijk : abc
{
class cde : abc.cde
{
}
}

and then it follows the normal rules of inheritance.

The only advantage to nesting classes in C# is to reduce scope (for
example declaring them "private") or to reduce name clutter in the
namespace (for example for a very specific class that can be used only
in one circumstance).

In particular, I believe that if you do this

class abc
{
class cde
{
}

public virtual cde CdeProperty
{
...
}
}

then in ijk, in order to override CdeProperty, you would have to say
this:

class ijk : abc
{
class cde : abc.cde
{
}

public override abc.cde CdeProperty
{
...
}
}

because if you were to say "public override cde CdeProperty" then you
would technically be declaring the property with a different return
type, ijk.cde, and you would require a "new" keyword, not "override".

I'm sure that others here will correct me if I have that all wrong. :)
 
A

ABC

It will be difference as I cannot know what properties will be added on the
initial stage.
 
B

Bruce Wood

If I understand you correctly, then yes, of course: that's what
inheritance is all about.

All I was saying in my post is that, from the point of view of
inheritance, this:

class abc
{
class cde
{
}
}

is exactly the same as this:

class abc
{
}

class cde
{
}

Other than considerations of access and naming, there is no difference
between those two code samples. Nesting cde inside abc does not give it
any special "inheritable" qualities.
 
J

Jon Skeet [C# MVP]

Bruce Wood said:
I may be wrong (and I'm sure that everyone here will jump all over me
if I am), but in C# declaring a class within another class does _not_
make the inner class an inheritable part of the outer class.

Absolutely right. Nor is there an implicit reference to an instance of
the outer class, as there is with inner classes in Java.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top