A B C of Access Control Modifiers

Ever wondered why A B C . . . is taught with reference to Apple, Ball, Cat and so on?

Wouldn't you agree that grasping something new is much easier and stays in your head for longer if it could be related to something you are very well familiar with?

During the placement season in college days, I remember myself trying hard to explain the otherwise straightforward and easy looking OOPS concept of access control modifiers to some of my friends. The terms Public, Private, Protected, Package (or Default), simply spelled confusion and chaos to some when it came to the application specific problems based on this concept. Here, I’ll try to simplify their explanation so that even a layman shall be able to relate to and feel comfortable with the concept.

There are four access control modifiers namely Public, Private, Protected and Package as we know in Java terminology.

To understand how access control modifiers work, lets consider the example of your residence. Not everyone would have the same level of access to the various objects/entities there. For example,

  1. Anyone can ring the doorbell or slip in a parcel into your mailbox.
  2. Your neighbors, friends, relatives i.e. known ones but not family members, have additional previleges in your house such as watching Television or admiring your furniture.
  3. Your family members shall have even more privileges, say to even enter your living room and may be access items from your cupboard.
  4. Finally, you yourself would have all privileges to access your stuff. Other than all mentioned above there might be items ONLY you would access say, your password protected business phone or personal system.
  5. The above mentioned points signify the Public, Protected, Package and Private access respectively. In these points, replace Anyone with any class, known ones but not family members with subclasses, family members with other classes in same package, yourself with the class that owns the entities, and you can easily deduce the explanation of mentioned access control modifiers. You may decide upon the level of access you want to allow for an entity and specify the access control modifier accordingly. The following matrix summarizes the concept and I am sure that from above example, anyone can comprehend and understand it:

    Public (doorbell)
    Y Y Y Y
    Protected (TV/Remote)
    Y Y Y N
    Package/default (Cupboard)
    Y Y N N
    Private (Personal System)
    Y N N N

    This article covers the concept at a very elementary level without getting into the code details. To explore more on this and other concepts of Object Oriented Programming, please refer to OOPS related books and online materials.

    PS: I dedicate this article to my Friend Sujit lalwani, the most deserving Young Indian Serial Entrepreneur, in the final stage of the Global Competition - YOUR BIG YEAR 2012. Kindly visit www.iuindia.com and VOTE for him before 9th Jan, 2012.

comments powered by Disqus