✅ inheritance
I understand what inheritance is and how it works in a language like java, but im having some trouble with the syntax in c#, particularly with constructors and taking arguments in them and the keyword base, which i cant quite figure out how to use
46 Replies
do you have an example of what you're trying to do that isn't working?
Yeah one second i gotta type it out
a C# constructor that calls a base constructor would look something like
I have an animal class with 2 attributes: size and legs (legs is a paramater, size is set to 5)
I want to create a chicken class that takes in a parameter for the legs, and had a size attribute of 3
What would the constructor for the chicken class look like
My animal constructor is
public Animal(int l) {
legs = l;
}
Both attributes are protected
since the attributes are protected you can set them both directly from the chicken constructor
but it would be good practice to call the base constructor to set the legs
How would i do that
the same way you would as if they were defined directly in the chicken class
My current constructor is
public chicken() : base(legs) {
size = 2;
}
yeah, something like that
But theres an error
what's the error?
oh, no legs
your
legs
variable needs to come from somewhereHow would i do that
public MyClass(string arg1, int arg2) : base(arg2)
It works
Thank you
:pepeok:
Actually
$close
If you have no further questions, please use /close to mark the forum thread as answered
Oh is this closed
Ok
Nvm
it's not
Oh
that's just a message saying to close it if you're done, but if you're not it's fine :LUL:
Whats the shorthand for getters and setters?
like the shortest way to define a property?
Whats the point in getters and setters there?
Its public
in that specific example it's a more technical reason, but just take my word for it that any public attributes should be properties and not fields
Whats the difference?
Like whats a property and a field
the actual compiled code is different, and it would be a binary breaking change to change an attribute from a field to a property or vice versa
properties are secretly methods that work on a hidden field
Right
it's basically a shorter way of using an attribute with getAttribute and setAttribute methods in java
I see
But why would you need getters and setters for a public variable here?
Or does that work with private and protected too
the visibility of the property and the visibility of each get/set can be different
for example, make it
private set;
so only the class can change it but anything can read itSo i can do
private var a { public get; public set; }
?
no, that wouldn't make sense
the getters and setters can't be more visible than the overall property, but they can be less visible
So how do i make a private attribute that has getters and setters i can use globally
that wouldn't be private at all
Ah
if you want to access it outside the class you'd want it to be public
So i should just use public attributes and ditch the getters and setters?
no, you should do what i showed earlier
if it's public, it should be a property (having get and/or set)
But why not
public string MyString;
Then i can access it with class.MyString
you can do the same with this, but like i said there are technical reasons this is better practice
basically it's easier to change in the future without breaking any code that may depend on it
So this works but your way is better practice?
right
I see
thanks for the all the help man
:pepeok: