T
Tony Johansson
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
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