Need some help understanding abstract

G

Gustaf

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
 
G

Gustaf

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.
}
}
 
M

Mattias Sjögren

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
 
M

Mythran

Gustaf said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top