11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15Protecting Members:Do Not DisturbIn This Chapter Declaring members protected Accessing protected members from within the class Accessing protected members from outside the classChapter 12 introduces the concept of the class. That chapter describes thepublic keyword as though it were part of the class declaration — justsomething you do. In this chapter, you find out about an alternative to public.Protecting MembersThe members of a class can be marked protected, which makes them inaccessibleoutside the class. The alternative is to make the members public.Public members are accessible to all.Please understand the term “inaccessible” in a weak sense. Any programmercan go into the source code, remove the protected keyword and do whatevershe wants. Further, any hacker worth his salt can code into a protectedsection of code. The protected keyword is designed to protect a programmerfrom herself by preventing inadvertent access.Why you need protected membersTo understand the role of protected, think about the goals of object-orientedprogramming:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!