Next thing to understand with regards to inheritance is about what members of a superclass can be inherited 2
by subclasses. 3
And this is dictated by access modifiers defined for those members which is cost access modifiers when 4
covering access levels and packages. 5
They were discussing about how one class can access other classes members. 6
But here we are talking about members that are inheritable except for one access modifier where that 7
is the purpose that access modifier. 8
There is no difference when it comes to accessing members of a class versus inheriting membership across 9
Britain or discussed Perpich remembered in detail as we did not cover subclauses at that instant of 10
time so protected modifier will be discussed in detail in this lesson. 11
This is an important topic not the end of the discussion. 12
I'm going to use some code to demonstrate this in the clearest possible way. 13
So let's look at the actual money first on how they can affect inheritance undergrad also serve as a 14
refresher to what we discussed earlier when other packages help and meaningfully organizing our classes 15
. 16
But as classes within a package will form similar kind of functions. 17
Sometimes some of these classes may interact with each of them however sometimes they may not interact 18
with each other but still provide a similar kind of functionality. 19
So all of us think of classes in a package as one close knit family access modifiers that allow us to 20
specify what data and behavior of a class is accessible to family members and non-family members or 21
family members being classis in the same package while non-family members are process reciting outside 22
of the package. 23
We will use this notional homily to explain some of the stuff in this lesson on how to access modifiers 24
which were discussed previously discussed private default and public modifiers as they don't behave 25
any differently from our earlier discussion. 26
The only difference is that protected Madhi where which is specific to inheritance and that we will 27
discuss next facias private modifier a private member which can be a private mailable or a private matter 28
of a superclass is never inherited by any subclass recalled that no class can ever access other classes 29
play much at all. 30
So no difference here. 31
In the case of inheritance. 32
Next is a member with default access level which means no explicit access modifier is used. 33
Default means only classes in the same package can access it. 34
That is only family members can access it. 35
So with regards to inheritance a default member is inherited by only subclauses in the same package 36
as the superclass. 37
Also the subprocess can only let other family members to access the inherited members so it is correct 38
to a major inheritance is involved or not a default member is never accessible outside the package. 39
Next is public modifier public means anybody can access it or anybody can inherit it. 40
If I inherited the inherited member can be accessed by anyone too that is. 41
Anyone can access the inherited member by using the subclass name or subclass object. 42
Nokomis interesting modifier that is specific to inheritance which is protected protected member has 43
same access libellous. 44
Default member but in addition it also provides special access to only subclasses. 45
Outside of the package Supertech protected member is primarily for use only in subprocess outside the 46
package ought to only family members on one or more of those family members can be subprocess to holler 47
. 48
One thing to remember is that the inherited protected member in a subclass should not be accessible 49
to any non-family member that is any class that is not in the same package as the superclass should 50
not access it. 51
We are the boss. 52
Hope that's clear. 53
But let's look at code and everything should be clear after that. 54
For the sake of convenience and browsers see it sometime. 55
I'm going to show this to my and my Eclipse editor and we're going to use 5 classes spread across two 56
different packages. 57
The reason for using two different packages is to demonstrate inheritance across packages. 58
That is how superclass in one package under subclasses can be in a different package the packages I've 59
named as follows. 60
One is Kondrat semantics Gweedore OOP Don't be one. 61
And the second one is calm down. 62
Semantics square dog OOP duck Pete. 63
Previously the package name was Kondrat semantics griddata basics on basics of democracy was under Busiek's 64
So there we were dealing with only language basic cities. 65
So the package name was basics. 66
But since we are dealing with object oriented programming I introduce this new package called OOP. 67
OK and it has two packages P1 and P2 under five classes are under Java beyond our Java Sea one bit to 68
unseat two class names ending with the suffix one going to the package be one. 69
So even once even going to prevent the ones ending with do with the suffix do which is B to C to D going 70
to the second package too. 71
So now let's look at the first class. 72
Are Java and de-classified different variables. 73
So in this demonstration I'm just going to use variables to demand strict inheritance accessibility 74
of inheritance but the same applies to methods also. 75
And this way the words are declared as static members but it need not be static which means that static 76
members are also inheritable. 77
So the first variable has the access modifier private under his name just pirate member. 78
The second one does not have any access modifier which means that it has default access level and it 79
is named as default member. 80
The third one is has protected access modified and it is nimblest protected member. 81
The final one is public. 82
Has public access modifier under it is named as public member. 83
No see when dodgiest simply extends A-one OK and it demonstrates accessibility of members to the inherited 84
members. 85
Do job which is in a different package. 86
Also extents. 87
Even so if one is in one package under a subclass it is in a different package and do Java is just a 88
regular class. 89
It does not extend even understood regler regular class but it demonstrates accessibility of variables 90
of members within A-one and doors that have been inherited in C so it also tries to access them and 91
also in C to also be true that Java is also like beyond or Java. 92
So it does not extend any class and B to C to out in the same package and it is also introduced just 93
to show accessibility of variables in a run. 94
And also the in and also to show how it can access variables that are inherited by C too. 95
OK so let's just begin with the first one which is a direct member. 96
So let's go to see one or Java and let me just uncommon this so C one is simply extending a one. 97
And here we get a compilation error. 98
When I holler what it is a feel A-one not direct memory is not visible. 99
So we know that private members are always private to the class itself so they are not inheritable. 100
I also they are not accessible from a different class. 101
So that so that's why if you're going to be taught Java on the file uncomment does class. 102
It says that one book pirate member is not accessible. 103
And of course same would apply for them both. 104
But once you do so on we don't have to test that. 105
Now let's go into one. 106
Let's try the next one which is default remember it's going to see. 107
Let me uncomment this and as you can see we still have the completion error because see too and it went 108
out in a different package and the default member which is defined and even is not accessible to because 109
default member is accessible only within the same package as a win. 110
So only to add to family members right. 111
But if you're going to see one or Java as you can see in this document default member is X is inherited 112
. 113
That's because you run and it went out in the same package. 114
And moreover the default member is accessed directly. 115
Right inherited members can be accessed directly now from being under Jawa you can access. 116
Default member. 117
Why not a one. 118
Because be run on are in the same package. 119
But interesting thing is that you can also access the inherited members to an inherited de-funded member 120
so you can access it YRC run also so both of these are possible. 121
And next is protected member. 122
So let's going to see to and as you can see the protected member is inherited by C too. 123
Even though C is in a different package so it can access a protected member So that's the main benefit 124
of declaring a variable as a protected right. 125
If it can be accessed by any subclass under subclass need not be in the same package. 126
Now that's going to be to do our Java and B cannot access the protected member in a one so protected 127
protected member has protected access level and it cannot be accessed so we get this compiler error 128
. 129
Now if I uncomment the second statement which is trying to access protected member YRC too. 130
So C to inherit protected member but it is still not accessible from B to and would be said that the 131
reason for that is because BE2 is not a family member. 132
Even so B is not in the same package. 133
Even so we discuss that also. 134
So let me just comment this back and let me go in to see what seaven. 135
Also can inherit the protected member you know that I don't see when it went out in the same package 136
back from B run. 137
You can access a protected member also is like a default member right. 138
It has the same access level on B when he went out in the same package so it can access protected member 139
. 140
It can also access the protected member YRC one so see if one is inheriting this protected member on 141
it can access it and see do you know that is in a different package and it is inheriting protected member 142
. 143
But Beaven is able to access or was not able to access it YRC do but be one is able to do it. 144
That's simply because A-one and B1 are on are one family and so that is that in the same package and 145
Beaven has the privilege to access protected member even YRC. 146
OK. 147
And the final one as a public member and public ments. 148
Anybody can access it and anybody can inherit it. 149
So C-2 can access here directly. 150
Which means you can inherit it and is able to access it directly and you also can access it from using 151
A-one and also using C-2. 152
So that's the benefit of being public so early that it was not able to access YRC to the protected member 153
while you do but the public memory can access. 154
So public is cruelly public it's part of the API. 155
Now see one also can access a public member and be one also can access public members either using one 156
or see one. 157
So that's about it. 158
The nice thing is we could also demonstrate not just our members that are inherited but also accessing 159
those members and the non-stop Grothus also accessing those members. 160
And that's it