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

Properties and abstract classes

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

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

Regards - Octavio

"MicroMoth" <st***********@forvus.co.uk> escribió en el mensaje
news:21**********************************@microsof t.com...
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

Nov 17 '05 #2

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

Nov 17 '05 #3

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

"Mattias Sjögren" wrote:
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

Nov 17 '05 #4

P: n/a
MicroMoth wrote:
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
"Mattias Sjögren" wrote:
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

Nov 17 '05 #5

P: n/a
"MicroMoth" <st***********@forvus.co.uk> wrote in message
news:E9**********************************@microsof t.com...
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
Nov 17 '05 #6

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

"MicroMoth" <st***********@forvus.co.uk> wrote in message
news:E9**********************************@microsof t.com...
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.

"Mattias Sjögren" wrote:
> 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

Nov 17 '05 #7

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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.