473,396 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Is this good OOP?

Consider the following class structure:

public class A
{
private X _x;

public X ProperyName { get { return _x;}}

public A()
{
_x = new X();
}
}
public class ASub : A
{
private XSub _xy;

public new XSub ProperyName { get { return base.X as XSub;}}

public ASub() : base()
{
_base.X = new XSub();
}
}

public class X {}
public class XSub: X
{
public string XsubVariable = "hello world";
}
As you can see there is a base class (A) and an derived class (ASub).
They both have a property (PropertyName), only ASub uses a property
type (XSub) derived from type (X), because the XsubVariable isn't
relevant for use by class A.

Although this works, i'm not certain if this is the right way to do
this, as _base.X is set two times, once in the constructor of A, and
once in the constructor ASub.

So, is this good OOP?

Bye,

Ward
Nov 16 '05 #1
5 1250
Ward,

I would say no, only because of the "new" construct. Personally, I
think that it is pretty abhorrent (from a design perspective). I don't like
the fact that it hides the base class definition, and allows me to get
around it with a cast to the base type. Basically, it throws consistency
out the window.

I would not shadow the property with new, rather, I would just have the
PropertyName property be virtual, and keep the field X as type X, only
overriding the constructor in ASub to create an instance of XSub.

In .NET 2.0, I would actually use generics to do this, if I wanted
something more strongly typed, like so:

public class A<T> where T : X, new
{
private X _x;

public A()
{
_x = new T();
}

public T PropertyName { get { return (T) _x }}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Ward Bekker" <wa**@NOequanimitySpaAm.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Consider the following class structure:

public class A
{
private X _x;

public X ProperyName { get { return _x;}}

public A()
{
_x = new X();
}
}
public class ASub : A
{
private XSub _xy;

public new XSub ProperyName { get { return base.X as XSub;}}

public ASub() : base()
{
_base.X = new XSub();
}
}

public class X {}
public class XSub: X
{
public string XsubVariable = "hello world";
}
As you can see there is a base class (A) and an derived class (ASub). They
both have a property (PropertyName), only ASub uses a property type (XSub)
derived from type (X), because the XsubVariable isn't relevant for use by
class A.

Although this works, i'm not certain if this is the right way to do this,
as _base.X is set two times, once in the constructor of A, and once in the
constructor ASub.

So, is this good OOP?

Bye,

Ward

Nov 16 '05 #2
Good idea! Luckly i'm developing on .Net 2.0 so this can be implemented.

Ward

Nicholas Paldino [.NET/C# MVP] wrote:
Ward,

I would say no, only because of the "new" construct. Personally, I
think that it is pretty abhorrent (from a design perspective). I don't like
the fact that it hides the base class definition, and allows me to get
around it with a cast to the base type. Basically, it throws consistency
out the window.

I would not shadow the property with new, rather, I would just have the
PropertyName property be virtual, and keep the field X as type X, only
overriding the constructor in ASub to create an instance of XSub.

In .NET 2.0, I would actually use generics to do this, if I wanted
something more strongly typed, like so:

public class A<T> where T : X, new
{
private X _x;

public A()
{
_x = new T();
}

public T PropertyName { get { return (T) _x }}
}

Hope this helps.

Nov 16 '05 #3
>> public new XSub ProperyName { get { return base.X as XSub;}}

I assume you meant that to be
public new XSub ProperyName { get { return base.ProperyName as XSub;}}

Also, note that as the classes are defined now, ASub.PropertyName (above)
will alwayws return null, as base._x is really a X object and cannot be
transforms into an XSub object.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Ward Bekker" <wa**@NOequanimitySpaAm.nl> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Consider the following class structure:

public class A
{
private X _x;

public X ProperyName { get { return _x;}}

public A()
{
_x = new X();
}
}
public class ASub : A
{
private XSub _xy;

public new XSub ProperyName { get { return base.X as XSub;}}

public ASub() : base()
{
_base.X = new XSub();
}
}

public class X {}
public class XSub: X
{
public string XsubVariable = "hello world";
}
As you can see there is a base class (A) and an derived class (ASub).
They both have a property (PropertyName), only ASub uses a property
type (XSub) derived from type (X), because the XsubVariable isn't
relevant for use by class A.

Although this works, i'm not certain if this is the right way to do
this, as _base.X is set two times, once in the constructor of A, and
once in the constructor ASub.

So, is this good OOP?

Bye,

Ward

Nov 16 '05 #4

James Curran wrote:
public new XSub ProperyName { get { return base.X as XSub;}}


I assume you meant that to be
public new XSub ProperyName { get { return base.ProperyName as XSub;}}


Yes, you're right, another small mistake: the constructor of ASub should
read:

public ASub() : base()
{
base.PropertyName = new XSub();
}

Doesn't change the issue though.
Also, note that as the classes are defined now, ASub.PropertyName (above)
will alwayws return null, as base._x is really a X object and cannot be
transforms into an XSub object.


Yes it can, as class XSub is derived from X. Because the constructor of
ASub sets it with an instance of XSub. This upcast is valid.

Bye,

Ward
Nov 16 '05 #5
"Ward Bekker" <wa**@NOequanimitySpaAm.nl> wrote in message
news:eC*************@TK2MSFTNGP11.phx.gbl...
public ASub() : base()
{
base.PropertyName = new XSub();
}
This will require a setter on A.PropertyName, which, until the release
of C# 2.0, must have the same visibility ("public") as the getter. And
now, you're creating an X object in the A ctor, then throwing it away and
creating a XSub object to replace it in the ASub ctor. That's messy and
wasteful, and not particularly good OO.

Let's see if we can clean this up a bit:

public class A
{
private X _x;

public X ProperyName { get { return _x;}}
protected A(X x) // only called by this class or subclasses.
{
_x = x;
}

public A() : this(new X())
{
}
}

public class ASub : A
{
private XSub _xy;

public new XSub ProperyName { get { return base.X as XSub;}}

public ASub() : base(new XSub())
{
}
}

Because the constructor of
ASub sets it with an instance of XSub. This upcast is valid.


You're right... I missed the assignment in the ctor..

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats...
29
by: RAY | last post by:
Hi , my boss has asked I sit in on an interview this afternoon and that I create some interview questions on the person's experience. What is C++ used for and why would a company benefit from...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
59
by: Alan Silver | last post by:
Hello, This is NOT a troll, it's a genuine question. Please read right through to see why. I have been using Vusual Basic and Classic ASP for some years, and have now started looking at...
17
by: Brett | last post by:
I'd like references on where to find some good (quality) icons to use for form and application icons (for the EXE). Most of the free stuff isn't that great looking and there isn't a good...
15
by: Alex L Pavluck | last post by:
I am new to programming other than SAS. I read that C# is a good starting language and I have started to create some simple programs with C# 2005 express edition. Can someone let me know if this...
6
by: Jamiil | last post by:
I am not a programmer by any means, but a dedicated aficionado. I have good understanding of Java and C/C++, and now I would like to learn javascript->ajax, but I don't know where to start. My HTML...
30
by: mistral | last post by:
Neeed good javascript unescape encoder, to protect javascript code. Some advices? Online tool, or ready javascript only. Any opinions about the Javascript Obfuscator:...
244
by: Ajinkya | last post by:
Can anyone suggest me a good compiler for(c/cpp) for windows? I tried dev cpp but its debugging facility is very poor.
76
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.