471,337 Members | 978 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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

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?
Jan 10 '06 #1
4 1533
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. :-)

Jan 10 '06 #2
ABC
It will be difference as I cannot know what properties will be added on the
initial stage.
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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. :-)

Jan 10 '06 #3
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.

Jan 10 '06 #4
Bruce Wood <br*******@canada.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

50 posts views Thread by Dan Perl | last post: by
2 posts views Thread by DotNetShadow | last post: by
8 posts views Thread by Per Bull Holmen | last post: by
5 posts views Thread by JH | last post: by
6 posts views Thread by Chris Fonnesbeck | last post: by
4 posts views Thread by RinKaMeAri | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.