PC Review


Reply
Thread Tools Rate Thread

about where to do the implementation for an interface

 
 
Tony Johansson
Guest
Posts: n/a
 
      29th Jan 2011
Hello!

Below I have an abstract class called Animal as you can see.
I have two questions about this.
1. I must be able to access some of the field from the derived classes what
is the best approach.
Is it to change the fields from private to protected or use public
property that the derived classes can access.

2.Here is an example of an interface that this class Animal implements
interface IAnimal
{
string ID { get; set; }
EaterType Eater { get; set; }
bool Aggressive { get; set; }
GenderType Gender { get; set; }
}

Now where is it most appropriate to do the concrete implementation for these
methods/properties
As I understand this if we for example take the first member ID of the
interface IAnimal we can make the concreate
implementation in the Animal class if we just want to return the ID that is
stored in the Animal class.
If we take the second member which is the property Eater we can do the
concreate implementation in the Animal class because
we only access the eater field
If we take the third member which is the property Aggressive we can do the
concreate implementation in the Animal class because we only access the
aggressive field.
If we take the fourth member which is the property Gender we can do the
concreate implementation in the Animal class because we only access the
gender field
Now in this example all concrete implamentations was done in the Animal
class.
So accoding to my knowledge we can always do the concreate implamentations
in the base class if the derived class hasn't any fields to add to the
implamentations

So can anyone give an good example when it would be nessessary to make the
concrete implementation of one of the methods/properties in one of the
derived classes ?


abstract class Animal :IAnimal
{
private string name;
private string id;
private double age;
private GenderType gender;
private HousingType housing;
private EaterType eater;
private bool aggressive;
private FoodItem food;

public Animal(string name, string id, double age, GenderType gender,
HousingType housing,
EaterType eater, bool aggressive, FoodItem food)
{
this.name = name;
this.id = id;
this.age = age;
this.gender = gender;
this.housing = housing;
this.eater = eater;
this.aggressive = aggressive;
this.food = food;
}

public string Name
{
get { return name; }
set { name = value; }
}

public string ID
{
get { return id; }
set { id = value; }
}

public double Age
{
get { return age; }
set { age = value; }
}

public GenderType Gender
{
get { return gender; }
set { gender = value; }
}

public HousingType Housing
{
get { return housing; }
set { housing = value; }
}

public EaterType Eater
{
get { return eater; }
set { eater = value; }
}

public bool Aggressive
{
get { return aggressive; }
set { aggressive = value; }
}

public FoodItem Food
{
get { return food; }
set { food = value; }
}

#region IAnimal Members abstract definitions

public abstract string AnimalLikesTheFood(string food);

#endregion
}

//Tony


 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      29th Jan 2011
On 29-01-2011 10:42, Tony Johansson wrote:
> Below I have an abstract class called Animal as you can see.
> I have two questions about this.
> 1. I must be able to access some of the field from the derived classes what
> is the best approach.
> Is it to change the fields from private to protected or use public
> property that the derived classes can access.
>
> 2.Here is an example of an interface that this class Animal implements
> interface IAnimal
> {
> string ID { get; set; }
> EaterType Eater { get; set; }
> bool Aggressive { get; set; }
> GenderType Gender { get; set; }
> }
>
> Now where is it most appropriate to do the concrete implementation for these
> methods/properties
> As I understand this if we for example take the first member ID of the
> interface IAnimal we can make the concreate
> implementation in the Animal class if we just want to return the ID that is
> stored in the Animal class.
> If we take the second member which is the property Eater we can do the
> concreate implementation in the Animal class because
> we only access the eater field
> If we take the third member which is the property Aggressive we can do the
> concreate implementation in the Animal class because we only access the
> aggressive field.
> If we take the fourth member which is the property Gender we can do the
> concreate implementation in the Animal class because we only access the
> gender field
> Now in this example all concrete implamentations was done in the Animal
> class.
> So accoding to my knowledge we can always do the concreate implamentations
> in the base class if the derived class hasn't any fields to add to the
> implamentations
>
> So can anyone give an good example when it would be nessessary to make the
> concrete implementation of one of the methods/properties in one of the
> derived classes ?


Those properties all belong to Animal because the backing field
is there so of course they should be implemented in Animal.

But if you have a property where the field is not in Animal
or a method that Animal can not implement, then the
implementation should be in the sub class.

A classic example:

public interface IAnimal
{
void Say();
}

public class Animal : IAnimal
{
public abstract void Say();
}

public class Dog : Animal
{
public void Say()
{
Console.WriteLine("Vov vov");
}
}

public class Cat : Animal
{
public void Say()
{
Console.WriteLine("Mjav majv");
}
}

Arne



 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      29th Jan 2011
On 29-01-2011 10:42, Tony Johansson wrote:
> Below I have an abstract class called Animal as you can see.
> I have two questions about this.
> 1. I must be able to access some of the field from the derived classes what
> is the best approach.
> Is it to change the fields from private to protected or use public
> property that the derived classes can access.


I forgot that one.

If the properties does not contain any functionality besides
accessing the field, then it does not have any practical
impact what you do.

If they do, then you need to decide on whether you want
that functionality or not when sub classes access the
fields.

If the properties are virtual then I would tend to prefer
the direct field access, because someone overriding those
will not know how they get called internally and that
could give some unexpected results.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Independent interface implementation Armin Zingler Microsoft VC .NET 3 7th Oct 2008 11:16 AM
Implementation of interface method is the same in all of the interface's implementations StevenECBrown@gmail.com Microsoft C# .NET 6 16th Mar 2007 11:44 PM
interface implementation stevenkobes@hotmail.com Microsoft C# .NET 5 9th Aug 2006 06:27 AM
Implementation of interface in C# =?Utf-8?B?Sm/Dq2wgRGVzY29tYmVz?= Microsoft Dot NET Framework 5 28th Apr 2005 01:36 AM
Vb.NET Interface implementation =?Utf-8?B?R3V5?= Microsoft VB .NET 4 27th Jan 2004 12:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:08 PM.