Reputation: 3876
I have a question regarding simple inheritance in C#.
Here is the code:
class Mammal
{
int age { get; set; }
public Mammal(int age)
{
this.age = age;
}
}
class Dog : Mammal
{
string breed { get; set; }
public Dog(int age, string breed)
: base(age)
{
this.breed = breed;
}
}
class Program
{
static void Main(string[] args)
{
Dog joe = new Dog(8, "Labrador");
Console.WriteLine("Joe is {0} years old dog of breed {1}", joe.age, joe.breed); // gives error
}
}
This gives error since it cannot access the age and breed parameters. So I make age and breed public in Mammal and Dog class respectively. This makes the program to run fine.
But my question is shouldn't ideally the parameters be made private or non-public and only accessed through public methods? If that's the case, then how can I access the non-public parameters in Program class?
Thanks
Upvotes: 1
Views: 139
Reputation: 13940
Having not provided an access modifier it uses it's default access. However, you're correct in most cases with your logic. You should hide as much as possible. So in reality I, depending on the situation, might provide public
getters and private
setters - or not. The point to get would be that you're free to do what you like as long as it delivers the correct results.
What you have here, in C#, is the creation of a variable that will always only be accessed via its getters and setters (implicitly of course) because that really is the best, although not so often implemented, practice.
So here:
[access] [type] [name] { [access]get; [access]set; }
we're able to control the access to the parameter but the parameter itself is seen. You could also, obviously because you've done it, make certain fields private that need not be seen, which is the not so implemented way. Often, variables take on some form of validation, no matter the access, so having private parameters like above is advantageous for you to use because the verification can be housed in the setters and allows for simpler syntax with MyClass.Something = somethingElse;
or System.Console.Write(MyClass.Something);
.
Upvotes: 0
Reputation: 8923
private
will never be available outside the class, you need to make it public
when you want to be accessible by all or protected
when you want your property/variable/method to be accessible only in inheriting classes.
Upvotes: 0
Reputation: 34844
The answer to how can I access the non-public parameters in Program class
is that you it can't (without doing reflection), because you can control what member variables, properties and methods are exposed to your derived classes and instantiated classes.
Data encapsulation is a good thing and you should use it to reduce the risk of calling code being able to modify data in your class that they should not have access to.
Upvotes: 0
Reputation: 4556
In your code, age
and breed
are properties. You can control access to the get
and set
methods separately by writing public get; private set;
. That would achieve what you want!
Additionally, consider whether it makes sense in your program to change the age and the breed of an object after it's constructed. If not, you can make them public readonly
fields.
Upvotes: 4
Reputation: 65087
Automatic properties are essentially methods (they are compiled to get_
and set_
methods). By marking them public
, they will be as you have described, which is fine.
You only need hide the information that an outsider shouldn't have access to. Your use case shows that an outside caller must have access to these properties, so marking them public
is fine.
If you don't want an outside caller to set
the value, then you can mark that particular accessor as private
or protected
.. whilst leaving the get
accessor public:
public int Age { get; private set; }
Also, uppercase the first letter in properties.
Upvotes: 5