By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,301 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,301 IT Pros & Developers. It's quick & easy.

Need some help understanding abstract

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a

"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.