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, was ability 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 categorised into four different types which are public, private,
protected and internal.
The most common ones are public and private, and you probably have used them 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
scope.
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 key word or it is automatically public
if you don't edit, but that's generally what you can do.
You can add the public keyword specifically state that a class or a method or a value even form a field
variable, however, is public.
Then the private modifier, and that's where things are could become a little more interesting.
Private modifier 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 Volks and the function do something are declared as private.
The class example is accessible from the same source file vault and fondue.
Something are only accessible within the sample 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 they're are really only available within that same class at 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
then turn on modifier as a feature.
And cotton, 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.
OK.
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 is 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 class base where we use this
protected veil I, which we set to zero.
So now we have this clause, which derives from base, so which is our derived class.
And there we have this function.
Get value for this method to be precise, and it returns an integer.
And there we now return I.
So we'll 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 that's called this Class C, then we cannot use this vow.
I.
Then another example of a overriding 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 bass.
It also has a function called get value and then it returns II.
But on top of that, it overrides the Value II.
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 class space where you make it open so that we can inherit from it.
Our other classes, like our derived class at the bottom, can inherit from it.
So we're a that we define as automatically public because it's public by default, right?
We didn't specifically state that it's public, but it is by default public.
Then we create this other variable called beam and that one is private.
So it's private to the base class.
Then we have this protected verbal.
See, and we make it open.
So this one is visible to the base and it 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 deriving.
It's visible there and it's usable there.
Then we have this internal Valda, 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 fund, which is our function, and it's visible to the base and the
draft class.
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 fund.
Main.
All right.
And there it's not a visible.
So here we have already arrived, plus a C, D and E of the base class 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 C because it's protected so it can be used in the draft class, but it's also open
so we can override it.
OK, this is protected, but we can override with the override.
Key word.
Then we have our function mate, 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.
PE is a private one, so it's not going to be visible anywhere else, right, except, of course, for
the base 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 forty four hour function.
All right, and then we have the vault derived object.
And here we are trying to access C, but C is not visible.
Why is that?
Well, because we are inheriting.
Our values see, we're overriding the 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, protected and 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.