Welcome back.
Now, at this point, we're finally ready to dive deep into access modifiers, because we have not done
that to a degree where I would have liked it to be, but it's only because one of them didn't make sense
yet.
So we saw public and we saw private and we saw how they worked.
Right now, let's have a look at how they work with deriving classes or with inheritance in general.
So we have this base class here which has three fields, right.
One is a public field, one is a protected field, and another one is a private field.
So public protected and private.
So these are the access modifiers okay.
Access modifiers.
All right.
And these are three different access modifiers.
There's a fourth one which is internal.
But when it comes to inheritance these three actually matter okay.
So public protected and private.
Now in this public method here void show fields I have the different variables and I'm just displaying
them onto the console now inside of my derived class.
So this is my child class and the base class here.
This is the parent class.
Slash the base class.
Well as you guessed it probably.
So now let's try to access the fields of the parent okay.
So let's try to access the public field, the protected field and the private field to see how they
behave.
Now first of all I'm going to override the public field.
Let's say I'm going to set one to it.
No problem at all I'm going to override the protected field.
You see, even that works.
And now I'm going to try to override the private field.
And you see I'm getting an error here.
So it says base class.
Private field is inaccessible due to its protection level.
So there you see what this private is for.
Private is there for only having access within the same class.
So whatever member is private is only accessible within the same class, no matter if it's a field,
if it's a property, if it's a method, if it has private in front of it, it's not going to be accessible
from outside of that particular class.
Okay.
Now in this case, you see that we cannot access the private field because the derived class cannot
access fields that are private.
Now, if you want a field to be accessible, you need to make sure that it's not private.
But if you don't want it to be publicly accessed, accessible to other classes except for derived classes,
then you can use the protected access modifier.
Okay, so that's where the protected access modifier comes in.
That's really the big game changer or differentiator to the other two.
So it's something in between the public and the private access modifier.
Public available to everyone.
Private only available to this class.
Protected available to every deriving class.
So every child class so to speak okay.
This counts for all members, right?
Not just for fields but also for properties and methods.
Okay.
So that's just something that I wanted to finalize with you and finally get into.
Now you've seen how to use it and when you can and when you can't.
Okay.
So now let's go ahead and create an object of our base class.
So base class my base class or let's call it base class new base class.
And we're going to call base class Dot.
And here we have this method show fields.
Let's run this.
And then also let's create an object of the derived class.
So derived class.
And I'm going to access its show fields.
So let's run this.
And you see public zero zero protected.
All of them are zero.
And that is because I'm not calling this method first.
So let's call this method to set the variables accordingly.
So here derived class dot.
And how did I call it access fields.
Right.
Okay there we are.
Now let's run this and we will see.
It says public zero protected zero, private zero and public one protected two and private zero.
So we could not override this value for private.
However, you can see that we can still see its value because we're not directly accessing it, but
we are accessing it over the show fields method.
So the show fields method.
Is exposing this private variable's value.
It's not exposing the variable itself, just the value of the private field.
And that's why we can see the private field even on our derived class show fields method, as this is
the parent show fields method, which itself exposed the value of the private variable.
But we don't have access to the private variable, the private field.
You can see that here.
We cannot overwrite the private field, and we cannot even access the private field at all.
So we could not say display the private field.
So here for example c w Console.WriteLine.
And then say something like private field and then display that.
We can't do that as it is private okay.
So this was like something that might have caught you off guard a little bit.
Maybe not, but I just wanted to make you aware of that side of things as well.
Okay.
So that's it for this video.
See you in the next one.