Wednesday 21 September 2011

why we never declare our class as private and protected .


                        

I promise that i never back to my promise , so when i decide something to do than i do it no matter what you think about me .

i asked this question to myself on 12th august and today is 22nd august , i find my answer in 11 days , takes too much time but ... for me its like a defeat of my another opponent . so lets come in the answer and start the explanation .

private :--- private , when we make a class , we think that when we built it generally send to JVM and nothing else is needed to run or process this class . but sorry we all are wrong , because there are 2-others type of classes which are needed to process any class , they are BOOTSTRAP classes and EXTENSION classes . so what is the task of these two classes , these classes are used at run-time means they provide the basic API to solve or execute our problem , and these API's are stored in jar files so if you make your class as private than no other class can access it , means BOOTSTRAP and EXTENSION classes also can not access it .
thats why this class can never be performed means never be executed so NO to PRIVATE class . for more information refer it (http://download.oracle.com/javase/1.5.0/docs/tooldocs/findingclasses.html#javalauncher)


protected :--- protected and default are same but a small difference , but which thing makes protected as special ,  now in this case (why we never declare our class as private and protected .) the speciality hurt protected ,
                we all know that protected is visible to outside the package but think when class is loaded at the JVM the object is created for ever class to perform actions now we have declare that we are protected and
visible to the outside the world with the help of inheritance . JVM only takes your class as a default but why as a default , JVM take you as a default because at JVM working is performed by object and everything is similar between DEFAULT and PROTECTED in the case of object means default == protected for object but , what for inheritance , inheritance is a positive point of protected but here protected breaks security so NO to PROTECTED .


Default :--- Default and protected are same at every thing but benefit of default is that , only package is visible to class loader , and thats why default is in use , and protected is out of the game because of its speciality . now why default is in game , when we perform any execution the class file inside this package is visible to the class loader so default is visible to JVM . means whole package is visible to the loader . so no problem for BOOTSTRAP and EXTENSION classes to access the whole package .

Public :--- public is simple , but used heavily in java , but why heavily because when you create anything that is built for help in some-other program means consider all API they all are created as public means they are open to all for use , and another reason that feasible to all means anyone can access it . so no problem for BOOTSTRAP and EXTENSION classes .

( please if you know more things about java and any question will rise in you mind than please share your question with comment , because i want to learn many things any random questions are the best way to learn anything )

No comments: