PC Review


Reply
Thread Tools Rate Thread

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

 
 
ABC
Guest
Posts: n/a
 
      10th Jan 2006
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?


 
Reply With Quote
 
 
 
 
Bruce Wood
Guest
Posts: n/a
 
      10th Jan 2006
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. :-)

 
Reply With Quote
 
ABC
Guest
Posts: n/a
 
      10th Jan 2006
It will be difference as I cannot know what properties will be added on the
initial stage.


"Bruce Wood" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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. :-)
>



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      10th Jan 2006
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.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10th Jan 2006
Bruce Wood <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialising/Deserialising subclass to base class Chris Ashley Microsoft Dot NET Framework 1 20th Jun 2008 11:35 AM
Inherit base class ? =?Utf-8?B?UGF1bA==?= Microsoft ASP .NET 0 20th Sep 2004 09:57 PM
variable type that can be declared in a base class this is accessible only to classes that inherit the base class Dan Microsoft ASP .NET 1 24th Mar 2004 09:34 PM
Pass an instantiated base class to a new class that inherits the same class -- Possible?? Brad Navarro Microsoft C# .NET 3 9th Jan 2004 01:07 AM
How convert a base class into a subclass?? Eidolon Microsoft Dot NET 5 3rd Nov 2003 05:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:26 AM.