Properties and abstract classes

G

Guest

Hi,
If I have an abstract class with variables in it, for example:
public abstract class A
{
private int A;
private int B;

public abstract void DoSomething();
}

Do I create the Property methods in the abstract class or the inheriting
class, for example

public class B : A
{
public override void DoSomething()
{
//
}

public int A
{
get
{
return a;
}
set
{
a = value;
}
}

public int B
{
get
{
return b;
}
set
{
b = value;
}
}

Or do properties belong in the abstract class?

Stephen
 
O

Octavio Hernandez

Hi,

I think properties belong to the abstract class, where the corresponding
fields exist.

Regards - Octavio
 
G

Guest

Do I create the Property methods in the abstract class or the inheriting
class, for example

The way you've written the A class now the properties have to be in A since
the backing fields are private. You'd have to declare them protected (or
something less restrictive) to access them from the derived class B.


Mattias
 
G

Guest

If I do put the Properties in the abstract class do I have to override them
in the same way as I would a method from an abstract class.
 
H

Hans Kesting

MicroMoth said:
If I do put the Properties in the abstract class do I have to
override them in the same way as I would a method from an abstract
class.

No. You don't *need* to override methods from an abstract class, if you
provide real code. Only "abstract" methods need to be overridden (unless the
derived class is abstract too).

Hans Kesting
 
R

Richard Blewett [DevelopMentor]

MicroMoth said:
If I do put the Properties in the abstract class do I have to override
them
in the same way as I would a method from an abstract class.

Well you can but there is no reason for them to be abstract just because
they are in an abstract class

public abstract class A
{
private int A;
private int B;

public int AProp
{
get{ return A; }
set{ A = value; }
}
public abstract void DoSomething();
}

is fine as is making the property virtual and allowing override. Making them
abstract has the additional problem of the backing store being private to
Class A and so inaccessible from derived classes (as Mattias pointed out)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
K

Kevin Spencer

An abstract class is simply a class which must be inherited. Other than
that, inheritance works the same way with derived classes; they inherit
everything in the base class. Abstrac methods and properties in an abstract
class do not contain a body, and therefore, the class which derives from the
Abstract class must implement the methods/properties, as well as defining
the bodies for them.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
J

Jeff Louie

Stephen... I should just mention that sometimes abstract classes are
best, other times using an interface is best. With practice it becomes
clearer when to use an abstract class or when to use an interface. So
it is a good thing to learn how to use both approaches.

Regards,
Jeff
 

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