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? 4 1628
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. :-)
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. :-)
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dan Perl |
last post by:
There is something with initializing mutable class attributes that I am
struggling with. I'll use an example to explain:
class Father:
attr1=None # this is OK
attr2= # this is wrong...
|
by: Gerry Sutton |
last post by:
Hi All!
I have noticed a strange behavior when using a constant identifier to
initialize an instance list variable in a base class and then trying to
modifying the list in subclasses by using...
|
by: phl |
last post by:
hi,
I am kind of confused aobut interfaces and abstract classes.
In short as I understand it, an interface is like a contract between
the class and the interface, so that certain funtions must...
|
by: DotNetShadow |
last post by:
Hi Guys,
I'm trying to work out how events work in VB.NET Basically I want to
create a base class that has an Event. I would like all derived
classes to inherit this event. I sorta worked out...
|
by: Per Bull Holmen |
last post by:
Hey
Im new to c++, so bear with me. I'm used to other OO languages, where it
is possible to have class-level initialization functions, that
initialize the CLASS rather than an instance of it....
|
by: JH |
last post by:
Hi
I found that a type/class are both a subclass and a instance of base
type "object".
It conflicts to my understanding that:
1.) a type/class object is created from class statement
2.) a...
|
by: orkonoid |
last post by:
Hello, I am having trouble with a Polymorphism issue using container classes.
I have a longwinded and shortwinded version of my question:
Shortwinded version:
How can I store base class...
|
by: Chris Fonnesbeck |
last post by:
I have a class that does MCMC sampling (Python 2.5) that uses decorators
-- one in particular called _add_to_post that appends the output of the
decorated method to a class attribute. However, when...
|
by: RinKaMeAri |
last post by:
Hi!
Could you imagine any way to block access to the base class public
methods?
Here is an example:
class B:
def public_method():
pass
class A(B):
def public_a_method():
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |