Now, let's look at something that is called visibility modifier or access modifier.
So in Scotland, they're called visibility modifiers, and they are the key word, which is used to
restrict the use of classes, interface methods and properties in Scotland.
So what is a visibility modifier?
Well, visibility modifiers are the key words which are used to restrict the use of classes, interfaces,
methods and properties in Scotland.
And these modifiers are used at multiple places such as the class header or the method.
Body and visibility modifiers are categorized into four different types which are public, private,
protected and internal.
The most common ones are public and private, and you probably have used him already or at least have
seen them already.
So let's start with a public modifier.
A public modified element is accessible from everywhere in the project.
The public modifier is the default modifier in Scotland.
So if any class interface etc is not specified with any access or visibility modifier, then that class
or interface or and so forth is used in a public school.
So if you had nothing, then that element is automatically public.
All public declarations can be placed at the top of the file.
If a member of a class is not specified, then it is by default public.
So here the same goes as for interfaces and classes and so forth.
So let's have a look at the syntax of public modifiers.
As you can see, you can make a class public by adding the public keyword or it is automatically public
if you don't edit.
But that's generally what you can do.
You can add to public keyword specifically state that a class or a method or a value, even for a field
variable, however, is public.
Then the private modifier, and that's where things could become a little more interesting, private
fire allows the elements to be accessible only within the block in which properties, fields, etc are
declared.
So if something is private, then it's only accessible where it is declared.
All right.
So the private modifier declaration does not allow access outside of the scope and the private package
can be accessible within that specific file.
So if you have multiple packages and you make one package, for example, private, then this one will
not be accessible in other files.
So, for example, if you have multiple classes and you have a package in one class and you make this
package private, then you cannot use it in other classes which are in other files.
All right.
So the syntax for a private modifier, well, you can have a class that is private, then you can have
its properties to be private by just adding this private keyword, then to methods to be private.
And and this above example, you can see that while X and the function do something are declared as
private, the class example is accessible from the same source file vol X and Fondo something are only
accessible within the example class.
So if we now create an object of this class example, we cannot access the variable X and we cannot
access the method to do something.
OK, so there are really only available within that same class and this class is only available within
that same file.
So if we use another file, this class will not be accessible and we cannot create objects of it.
Then the internal modifier is a feature and Cottan, which is not available in Java and the internal
modifier makes the field visible only inside the module in which it is implemented.
So all fields are declared as internal, which are accessible only inside the module in which they are
implemented.
All right, so another example here, and we can see the syntax for internal modifiers, so you can
use this internal keyword for a class, for a property and also for methods and even for global variables,
as you can see here.
So then there is a keyword called open, and we need to talk about it real quick in order to understand
the next concept.
So in Scotland, all classes are final by default, so they can't be inherited by default.
It's different to other programming languages.
For example, in Java, it's the opposite that you have to make your class final explicitly because
all classes can be inherited from.
So if you want to use inheritance, then you need to make the class that you want to inherit from open.
So to make a class inheritable to other classes, you must market with the open keyword and otherwise
you get an error here type as final, so can't be inherited.
All right.
So then there is the protected modifier with a class or an interface allows visibility to its class
or subclass.
A protected declaration, when overridden in its subclass is also protected unless it is explicitly
changed.
So we're going to look at that in a bit.
And the protected modifier cannot be declared at the top level, so packages cannot be protected.
You can use other visibility modifiers, but you cannot use the protected modifier on packages.
So here's an example.
You can see here we're using the open keyword open space where we use this protected well eye, which
we said to zero.
So now we have this class which derives from base, so which is our derived class.
And there we have this function get value or this method to be precise, and it returns an integer.
And there we now return.
I so will return the variable that was created in the base class because we are inheriting from it,
we can use it.
But if we weren't inheriting from it, so if we're in another class, let's call this class C, then
we could not use this.
Val, I.
Then another example of a writing of protected types, so here we have an open protected value.
So now it allows us to override that value.
OK, so now we can override it.
We cannot just only use it, but actually override it.
So you can see we have another class which also inherits from base so different to the one that we had
before.
As you can see, this was class derived and now we have this class called another.
It derives from base.
It also has a function called get value and then it returns.
I put but on top of that, it overrides the value.
So here you can see if you want to be able to write something, you need to add to open keywords to
it.
So in this case, you need to open the value that we use there, the variable.
All right.
Now, we have another example here of the visibility modifier.
So we have this open space.
We make it open so that we can inherit from it our other classes, like our derived class there at the
bottom can inherit from it.
So Vaudrey that we define as automatically public because it's public by default.
Right.
We didn't specifically state that is public, but it is by default public.
Then we create this other variable called P and that one is private.
So it's private to the base class.
Then we have this protected variable.
See, and we make it open, so this one is visible to the base and at the derived class, which means
that the derived class in this case, it's called drive, but this is the case for all classes that
are driving.
It's visible there and it's usable there.
Then we have this internal valdete which is visible inside the same module, which means that it is
visible inside of the same file pretty much.
And then we have this protected fun e, which is our function, and it's visible to the base and the
rest plus.
So because it's protected, it's not only visible or usable in our base class, but also in our derived
class, but not inside of our main.
All right.
And there is not a visible.
So here we have our drive, plus A, C, D and E of the base, clouds are visible, B is not visible.
Why is that?
Well, because it's private, so it's only available in the base class itself.
Protected internal and public variables, however, are visible in the draft class.
And then we can even override the value, see, because it's protected so it can be used in the draft
class, but it's also open so we can override it.
OK, so he's protected, but we can override it with the override keyword.
Then we have our function main, and in there we have a base object, so we create an object of our
base class and we create an object of our derived class.
All right.
So when we try to access A and D., we can access them because A.
S public and D.
His internal so it's visible in the same module and we are in the same module in this example, and
then we have base B, C and E, which are all not visible because they are protected.
So they are only visible in the same class or in a driving class.
All right.
So.
B is a private one, so it's not going to be visible anywhere else, right, except, of course, for
the best class.
Then we have to value see which is protected.
So it's not visible outside of classes that are driving and the class itself.
And then we have the same for E for our function.
All right, then we have the vault derived object and here we are trying to access see but see, it's
not visible.
Why is that?
Well, because we are inheriting.
Our values see we're overriding that value, see, but it's still going to be protected, so C is not
visible.
So this is a little bit much to digest, I can imagine.
So no worries.
But we're going to use this quite a bit.
So we're going to use all of these visibility modifiers when we need them.
And most of them we are going to use very sparingly, very rarely.
So, for example, protectant, internal are not used as often as, for example, the private key word.
So we're going to use that one.
And step by step, they all will become natural.
So no worries if that was a little bit much at all at once.
But I wanted to keep it all together in one video so you can see the different rules there.