Hello, dear students, in this lesson, we'll talk about encapsulation and we'll learn what is the
role of encapsulation principle in the object oriented programming.
In this lesson, we'll talk with you.
Why do we need encapsulation and how we can implement encapsulation in our programs?
We'll learn with your access modifiers and on real court examples, I will show you how you can use
them.
I will share with you my practical experience when to use which modifier and will learn the rules which
we need to follow during overwriting.
So let's jump to examples to understand this topic better.
What is an observation?
Encapsulation is a principle that teaches us to group data and behavior those our fields and methods
together.
As a single unit, we can control access to interfaces, classes, fields, methods to control access.
We have four main access modifiers.
They are public protected package, private or default private.
Let's start one by one to understand the difference on the specific examples, to not show you how access
modifiers work with classes, fields and methods.
Separately, I will show you how Access Modifier works with methods, because these modifiers control
access to elements and the way how they work are similar, no matter whether you're trying to get access
for field or method.
OK, so imagine we have some class, let's call it Class A. And inside this class we have the method
with public access modifier.
That means you can access it from any point of your program.
For example, here you can see them a class which is outside of our package.
No.
One, I can create the instance of Class A and call the public method on it.
Now imagine I have protected MassArt.
Do something to will I be able to call it from them a class?
Unfortunately, no way, because I can get access to protected entities only inside the same package
or in a child class.
If only I had them a class inside the package, no one I would be able to call method to do something
to see here.
I can call this method when demo class is in the same package with class A.
Also here I have a class of the class A, package number two and inside the do something class B I can
call protective measures.
Do something too.
So now you understand that I can access protected entities in case they're in the same package or in
child classes.
Our next modifier is package private.
You can see that do something.
Class B doesn't have any access modifier specified.
That is not true.
When we don't specify access modifiers, that means that access to this entity is a default.
So usually absence of any keywords are associated with access.
Modifier is called package primary or just default access.
So let me get back to them.
A class and package number one in scope of this package, I will create the object of the Class B type.
And on this object I call do something Class B method and you see that I have a compilation error.
But if I create object of Class B type inside the package number two and call the same method, it is
possible because default access is only within the same package.
Take into account this demo class and Class B in the same package.
We don't have compilation error and last but not least, modifier is private modifier.
Everything is easy here.
You can access private entities only in scope.
When it was declared, for example, I have private method declared in this class and I can call this
private method wherever I want in this class, but I can't call this method outside of the class, even
in the same package.
If I will try to do so, I see compilation error.
So now you understand the difference between modifiers.
I believe you understood that encapsulation in all AP language can be implemented with the help of access
modifiers and you know how they work.
Knowing this during the planning of architecture, you can choose access modifier, which works the
best for you.
I want to share with you, which modifies you will use most of the times in 99 percent of cases you
will use is a private or public.
This is because you may have very few business cases when you want to keep something visible only in
scope of the package or only in scope of the package and child class.
Most likely you will implement interfaces and would like to keep the behavior public.
Sometimes you will have methods which are needed only for this class and you will keep them private.
Usually all fields have the private access modifier because you want to control interaction with your
object.
So now you understand that default and protected modifiers are not used very often.
And last thing for the day, which I wanted to discuss with you, is the rules for access modifiers
during that method.
Overriding the rule is simple.
When you override the method, you can't reduce the visibility of the inherited method.
For example, in class a way of do something method.
Class B extends class A and class C extends class B, is that clear?
And now I want to do something method and I override it was protected to modify it just to recall it
was public and parent class.
And I have a compilation error here because we can't narrow access.
On the other hand, in Class B, do something.
Class B method has default access modifier.
I can increase access level.
And you can see here that I overwrite my head with public modifier and there is no compilation error
because you can extend access now, that's it.
Now, let's recall what we have learned today.
Today, we learned what an encapsulation is.
We know that access modifiers can help us to implement encapsulation.
Principle in our code also will learn the difference between public protected default and private access
modifiers.
We discussed which modifiers developers use more often, and we saw specific rules which we need to
follow during override.
And that's all what I had in my agenda for today.
Thanks a lot for your attention.
And see you in the next lesson.