Let's not look at a very important topic. 2
Access levels not really understand what packages are what classes are and also what members construct 3
ridiculous like reliables methods and so on. 4
But our main goal is to build very design software and for that there are certain design principles 5
in the next lesson they will learn about one such design principal information hiding because a name 6
suggests has something to do with hiding off information. 7
For example women not want our variables to be accessed directly. 8
That is we want to hide our variables. 9
For instance if you recall our student example we were accessing the name variable in the Student class 10
directly. 11
And that is a bad practice as we will see when learning about information hiding So you talking about 12
restricting access to some of the stuff like Redeemer's information hiding is about how to provide such 13
restrictions on to achieve it. 14
It uses the concepts that we learn in this lesson so let's go ahead and learn about access elements 15
access limits basically help in providing restrictions on accessing classes on their members. 16
For example when it comes to classes V.M. want a class to be accessible only to classes within the same 17
package. 18
That is we don't want our class to be accessible outside of it's package for example we may not want 19
classes outside the package to create an instance of one particular class. 20
That is we want the class to be instantiated only by classes in the same package. 21
So that's a kind of restriction however in most cases you would want the class to be accessible to every 22
other class. 23
There is two classes in the same package as well as two classes outside of each package. 24
And for this to happen you will include a key word called public in the class declaration and here is 25
how it would be when used in the class declaration for basics to modeless hit public is referred to 26
as an access modifier and it should come before the key word us now if you place it in between what 27
class and the class name then you get a compiler error. 28
In fact you cannot have anything in between. 29
The key word class name so you use public modifier. 30
If the accessibility of the class is public. 31
But for the previous kids their accessibility is only within the package. 32
We do not use any modifier So that's not the focus. 33
So if you don't use any access modifier then it implies that the class is accessible only within the 34
package. 35
Also you know the title is accessibility for book classes as well as interfaces interfaces and important 36
concepts like class and will be introduced when discussing object oriented concepts. 37
Similitude there are certain restrictions when it comes to members of a class like reliables methods 38
constrictors or even member classes or member interfaces which will be introduced later. 39
Let's see what these distinctions are are one restriction is for a member of that class to be accessible 40
only within the class in which it is defined that is outside of the class that members should not be 41
accessible. 42
For example you may want a variable to be accessible only within that class like an arts methods are 43
constructors but not to any code outside of the class. 44
This you would use an access modifier call when declaring the variable. 45
It's not just that the member is private but across 46
sometimes you may want a particular member to be accessible not only with the next class but also to 47
other classes in the same package. 48
For this we do not use any access Madhi where sometimes this access level is also referred to as package 49
private as the accesses private with a package. 50
We may also want a member to be accessible to all classes in the same package like in the previous case 51
. 52
But in any Sheean we may want that member to be accessible to all subclasses of its costs on those up 53
classes maybe outside the package do some classes are not discussed and they will be introduced to encumbering 54
inheritance which is an object oriented concept. 55
So let's start with you. 56
What's the process for now but not at this access level. 57
You would have to use an access modifier called protected when declaring the variable finally be me 58
I also want certain members of the class to be accessible to any court regardless of where the court 59
is. 60
There is a court maybe inside the package or outside the package too. 61
So that's the public access level and you would use an access modifier called public when declaring 62
that even know that anything declared with public Marty for being a class or interface or any of their 63
members that particular member would become part of the API as any code outside the package can also 64
access it. 65
Just know that these access levels are ordered from most to least restrictive. 66
So first one is most strict so as visibility of class member is only within that class. 67
Similarly last one is at least a strip built as a class member is accessible to any quarter ourself 68
all the money for our private and public are to use most of the time it is used relatively rarely and 69
default package by what access is also commonly used but maybe not as much as private and public finally 70
know that interface's also have members like we deal with some matters but it's not really about their 71
accessibility for now. 72
We will discuss when we get to interface's So as you can see when compared to classes and interfaces 73
members of a class have two additional access level restrictions that is private unprotected restrictions 74
public and default access levels apply to both classes as well as their members. 75
Let's let's just discuss a little bit more about only private modifier as there is one point I would 76
like to highlight regarding private modifier. 77
Let's see hope private modifier is used with various methods. 78
The same applies to other modifiers too. 79
So you don't have to look at them explicitly. 80
If you're declaring a variable that's private you would use a modifier private. 81
So not this variable would be visible only with the next class also the private variables can be defined 82
at class level on me or not matters live in any variable defined in a method that is a local variable 83
is already private to the ometer so using public modified with a local variable will give you a compiler 84
Similarly you can find a method as a pirate. 85
Not that he had to modify a pirate cannot be specified in between the methods returned by the method 86
name. 87
It has to be specified before it might otherwise be get a compiler. 88
You just cannot have anything in between the mattress. 89
You can type on them that's name next is the point I wanted to highlight using private modifier implies 90
that it is. 91
But I work with a class not by weight to objects of its class. 92
Let's look at an example. 93
This is a stripped down version of our student class example. 94
Here we have a student class variable ID declare that's private and disgress has two objects reference 95
by variables S1 and S2. 96
There is also the class course with its own object. 97
Not you can see object Essman can access I'll be full of objects as to even though ideas by to s2. 98
That's because objects of a class can access each other's private stuff. 99
Hoher the class course or even its object cannot access the ID field of either student class or any 100
of its objects. 101
So once again if something is declared private It means it is private goods class or not but the objects 102
of its class you may also recall that when introducing packages it was mentioned that one use of package 103
is security so that you can see here by using access modifiers like private we restrict what parts of 104
the code can be seen outside the class. 105
And of course outside the package too. 106
So that's the whole discussion on access modifiers. 107
Our next as we mentioned earlier they will look at an important design principle called information 108
hiding which is a tool by using access modifiers we would also look at a couple of important effective 109
items in understanding information hiding post offical wedding information hiding he had them all with 110
the student example that we talked earlier will be enhanced the enhancement will be based on what we 111
learn and information hiding there we will be using access modifiers. 112
And so you will see them in action in that demo. 113
Thank you.