Tuesday, June 4, 2013

Abstraction

Abstraction in OOPs means, hiding or abstracting something. Abstraction helps in hiding requirements which are not required for the actual implementation; this might sound similar to encapsulation. Yes Encapsulation and Abstraction concepts complement each other.

Abstraction tends to hide the full complexity of the system form the external world and show only the details which are required to use the features exposed by the object. The actual functionality might depend on many other parameters which are not required to be exposed to the outside world; abstraction hides these and shows only those details which are required to use the system.

The following class explains the concepts of Abstraction.
The class hides the variables strName, nAge and the function calculateEmployeeSalery by marking them as private and shows only the required details to the outside world.

        // Abstraction & Encapsulation
        public class clsEmployee
        {
            // private variables Hidden from outside
            private string strName;
            private int nAge;
            //
            // Public Properties
            public string Name
            {
                get
                {
                    return strName;
                }
                set
                {
                    strName = value;
                }
            }
            public int Age
            {
                get
                {
                    return nAge;
                }
                set
                {
                    nAge = value;
                }
            }
            //
            // Public Methods
            public int getEmployeeSalery(int empId)
            {
                return calculateEmployeeSalery(empId);
            }
            //
            private int calculateEmployeeSalery(int empId)
            {
                int nSalery = 0;
                // Logic to calculate Salery
                return nSalery;
            }
        }


Search Flipkart Products:
Flipkart.com

No comments: