Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Abstraction and Encapsulation ( Chapter-4)

Abstraction and Encapsulation




Ø  Abstraction and encapsulation are important features of any object-oriented programming language.
Ø  Abstraction involves extracting only the relevant information.
     Ø  Encapsulation involves packaging one or more components together


Encapsulation

Ø  Encapsulation literally means ‘to enclose in or as if in a capsule’.
Ø  Encapsulation is defined as the process of enclosing one or more items within a physical or logical package.
Ø  It involves preventing access to nonessential details.

Implementing Encapsulation by Using Access Specifiers

Ø  An access specifier defines the scope of a Class member.
Ø  A class member refers to the variables and functions in a class.
Ø  A program can have one or more classes.
Ø  You may want some members of a class to be accessible to other classes.
Ø  But, you may not want some other members of the class to be accessible outside the class.

Types of Access Specifiers

Ø  C# supports the following access specifiers:
v  public
v  private
v  protected
v  internal
v  protected internal

Using Methods

ØA method is a set of one or more program statements, which can be executed by referring to the method name.
ØTo use methods, you need to:
v  Define methods
v  Call methods

Defining Methods

Ø  Defining a method means declaring the elements of its structure.
Ø  Consider the syntax of defining a method:
(Parameter List)
{
                Method Body
}
Ø  The elements of the method declaration include the method name, the Parameters list, the return type, and the method body.
Ø  The following are the elements of a method:
v  Access specifier:  This determines the extent to which a variable or method can be accessed from another class.
v  Return type: A method can return a value of any type. If the method is not returning any value, use void as the return type.
v  Method name : is unique. It cannot be the same as the variable name or any other
non-method item declared in the class.
v  Parameter list : is used to pass and receive the data from a method. It is enclosed between parentheses. The parentheses are included even if there are no parameters.
v  Method body :It contains the set of instructions needed to complete the required activity.

Calling Methods

Ø  After defining the method, you can execute it by calling it.
Ø  You can call a method by using the name of the method.
Ø  The method name is followed by parentheses even if the method call has no parameters, as shown in the following example:
MethodName();

eg:- Class Demo
{
public void display()
                {
                Console.WriteLine(“Hello World”);
                }
                                public static void main(String [] args)
{
Demo obj=new Demo();
obj.display();
}
 }

Methods with Parameters

Ø  Methods can also be declared with parameters. Consider the example of declared a method with parameters:
void DisplayResult (int result)
{
//…..
}
Ø  When the methods are declared with parameters, they should be called with parameters. The methods with parameters are called by passing the value using the following mechanism:
v  Value
v  Reference
v  Output

Ø  Value: The Parameters Passed by value creates a separate copy in the memory. The following example shows the parameters passed by value:
void Sum( int num1, int num2)
{
//…        
}
void Accept()
{
int val1=10;
int val2=2;
Sum(val1,val2);
}

Ø  Reference: The parameters passed by reference does not creates a separate copy of the variable in the memory. A reference parameter stores the memory address of the data member passed. The following example shows the parameters passed by reference:
void Sum( ref int num1,ref int num2)
{
                //…        
}
void Accept()
{
                int val1=10;
                int val2=2;
                Sum( ref val1,ref val2);
}

Ø  Output: The output parameters are used to pass the value out of the method. The following example shows the parameters passed by reference:
void Sum( ref int num1,ref int num2, out int result)
{
result=num1+num2;     
}
void Accept()
{
                int val1=10;
                int val2=2;
                int recieveVal;
                Sum( ref val1,ref val2,out recieveVal);
}


Kindly click on Follow me   for more updates

Kindly share this link to your friends on Facebook,Whats up,Twitter,Google+ .

http://www.technotechmedia.com/2016/07/abstraction-and-encapsulation-chapter-4.html




This post first appeared on Pivot In SQL Server, please read the originial post: here

Share the post

Abstraction and Encapsulation ( Chapter-4)

×

Subscribe to Pivot In Sql Server

Get updates delivered right to your inbox!

Thank you for your subscription

×