473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Properties and abstract classes

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
7 2568
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
2
by: Paul Selormey | last post by:
I have looked through the documents but could not find any information on this. Is anything like static properties in interfaces? If not, how do I define property in interface to be made static...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
4
by: Gert Kok | last post by:
The microsoft page http://msdn2.microsoft.com/en-us/library/9fkccyh4.aspx states: Remarks (nr 4) Virtual properties behave like abstract methods, except for the differences in declaration...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I am actually a VB.Net guy, but I have worked somewhat with C++ and C#. I just want to ask about the relationship between Abstract Classes and Interfaces. My first question is if...
4
by: Josh Valino | last post by:
Hi group, Is there a way in C# (.Net 3.5 FW) for me to define an abstract class that has some properties, and then in each derived class, select which properties I'd like available and which...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.