471,330 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Need some help understanding abstract

I have a class that I need to adapt to various scenarios. Some
properties and methods will be needed in every case, while other's are
unique for one case. So I made a base class, and a set of other derived
classes for each scenario.

But I don't understand this fully. There are not only methods in my
original class. It has data too, in private fields.

public class OriginalClass
{
private string field1;
private string field2;

// Constructor
public OriginalClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
}

public void ProcessData()
{
// Using field1 and field2 here.
}
}

Does this mean it's no use having fields in an abstract class? I wonder
if I have to write derived classes so that each method takes all the
data necessary as parameters? Like this:

public class BaseClass
{
// No private fields and no constructor

public void ProcessData(string field1, string field2)
{
// Using field1 and field2 here.
}
}

public class DerivedClass
{
private string field1;
private string field2;

public DerivedClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;

ProcessData(field1, field2);
}
}

Gustaf
Feb 6 '06 #1
3 1079
Sorry, I forgot abstract here:

public abstract class BaseClass
{
// No private fields and no constructor

public void ProcessData(string field1, string field2)
{
// Using field1 and field2 here.
}
}
Feb 6 '06 #2
Does this mean it's no use having fields in an abstract class?


No it doesn't. You can certainly have fields in acstract classes too.

Of course if you want derived classes to access them you should
declared them as protected or exposed them via properties.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 6 '06 #3

"Gustaf" <gu*****@algonet.se> wrote in message
news:Ov*************@TK2MSFTNGP10.phx.gbl...
I have a class that I need to adapt to various scenarios. Some properties
and methods will be needed in every case, while other's are unique for one
case. So I made a base class, and a set of other derived classes for each
scenario.

But I don't understand this fully. There are not only methods in my
original class. It has data too, in private fields.

public class OriginalClass
{
private string field1;
private string field2;

// Constructor
public OriginalClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
}

public void ProcessData()
{
// Using field1 and field2 here.
}
}

Does this mean it's no use having fields in an abstract class? I wonder if
I have to write derived classes so that each method takes all the data
necessary as parameters? Like this:

public class BaseClass
{
// No private fields and no constructor

public void ProcessData(string field1, string field2)
{
// Using field1 and field2 here.
}
}

public class DerivedClass
{
private string field1;
private string field2;

public DerivedClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;

ProcessData(field1, field2);
}
}

Gustaf


Here is an example of a base, abstract class that contains private members:

public abstract class BaseClass
{
private string field1;
private string field2;

protected BaseClass(string field1, string field2)
{
this.field1 = field1;
this.field2 = field2;
}

// Make field1 visible to derived classes, and readonly as well.
protected string Field1
{
get {
return field1;
}
}

protected int DoSomething
{
// Do something with field2.
return field2.Length;
}
}

public class DerivedClass : BaseClass
{
public DerivedClass(string field1, string field2) : base(field1, field2)
{ }

public void ProcessData()
{
string field1 = this.Field1;
int field2Length = this.DoSomething();
}
}

:)

Mythran

Feb 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bonj | last post: by
4 posts views Thread by vidalsasoon | last post: by
2 posts views Thread by Rahul | last post: by
11 posts views Thread by Bryan Kyle | last post: by
10 posts views Thread by satan | last post: by
Dormilich
2 posts views Thread by Dormilich | last post: by
reply views Thread by rosydwin | 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.