Difference between public private and protected


 Key difference is


in public access modifier, we can access the data type anywhere in the project with extending that class.

In protected access modifier, we can access the data type anywhere but in other class, we have to inherit that class.

In default, we can access that variable in that same package only.

In private access modifier, we can access that variable only in that class and not in any other class even by inheriting them.


**********************************Example**************************************








the picture shows that by making the object of the same class we can access the public and protected variables of the parent class.

Note:- please do it by yourself in your pc to get understanding .. as if you will not do.. u may not get up to what which you should. Next, it is up to you. My work is done.



***************************************Example***********************************

Now, we are not extending to parent class present in another package.






In this case, we have to make an object of that class whose variable we need to fetch.

thus only public variable is accessible here.

***********************************Example ***********************************

Now, we are accessing a private variable in another class.






In this case all public, protected and default variable is accessible but not the private one.






























No comments:

Post a Comment