03 iec t1_s1_oo_ps_session_04

26
Slide 1 of 26 Session 4 Ver. 1.0 Object-Oriented Programming Using C# In this session, you will learn to: Define abstraction and encapsulation Implement encapsulation by using access specifiers Use methods Objectives

Upload: niit-care

Post on 12-Jul-2015

406 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: 03 iec t1_s1_oo_ps_session_04

Slide 1 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

In this session, you will learn to:Define abstraction and encapsulation

Implement encapsulation by using access specifiers

Use methods

Objectives

Page 2: 03 iec t1_s1_oo_ps_session_04

Slide 2 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

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.

Introducing Abstraction and Encapsulation

Page 3: 03 iec t1_s1_oo_ps_session_04

Slide 3 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

An automobile salesperson is aware that different people have different preferences.

Some people are interested in the speed of a car, some in its price, some in the engine, and the some in its style.

Although all of them want to buy a car, each of them is interested in a specific attribute or feature.

The salesman knows all the details of a car, but he presents only the relevant information to a potential customer.

As a result, a the salesman practices abstraction and presents only relevant details to customer.

Defining Abstraction

Page 4: 03 iec t1_s1_oo_ps_session_04

Slide 4 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

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.

Defining Encapsulation

Page 5: 03 iec t1_s1_oo_ps_session_04

Slide 5 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

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.

Implementing Encapsulation by Using Access Specifiers

Page 6: 03 iec t1_s1_oo_ps_session_04

Slide 6 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

C# supports the following access specifiers:public

private

protected

internal

protected internal

Types of Access Specifiers

Page 7: 03 iec t1_s1_oo_ps_session_04

Slide 7 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Problem Statement:Write a program to calculate the area of a rectangle and a square.

Demo: Calculating Area and Volume by Using Access Specifiers

Page 8: 03 iec t1_s1_oo_ps_session_04

Slide 8 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

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:Define methods

Call methods

Using Methods

Page 9: 03 iec t1_s1_oo_ps_session_04

Slide 9 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Defining Methods

Defining a method means declaring the elements of its structure.

Consider the syntax of defining a method:<Access specifier> <Return Type> <Method Name>(Parameter List)

{

Method Body

}

Page 10: 03 iec t1_s1_oo_ps_session_04

Slide 10 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Defining Methods (Contd.)

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:Access specifier

Return type

Method name

Parameter list

Method body

Let us understand each of the element of the method declaration.

Page 11: 03 iec t1_s1_oo_ps_session_04

Slide 11 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

This determines the extent to which a variable or method can be accessed from another class.

Defining Methods (Contd.)

Defining a method means declaring the elements of its structure.

Consider the syntax of defining a method:<Access specifier> <Return Type>

<Method Name>(Parameter List)

{

Method Body

}

Page 12: 03 iec t1_s1_oo_ps_session_04

Slide 12 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Defining a method means declaring the elements of its structure.

Consider the syntax of defining a method:<Access specifier> <Return Type>

<Method Name>(Parameter List)

{

Method Body

}

A method can return a value of any type. If the method is not returning any value, use void as the return type.

Defining Methods (Contd.)

Page 13: 03 iec t1_s1_oo_ps_session_04

Slide 13 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Defining a method means declaring the elements of its structure.

Consider the syntax of defining a method:<Access specifier> <Return Type>

<Method Name>(Parameter List)

{

Method Body

}

This is a unique identifier and is case-sensitive. The method name cannot be the same as the variable name or any other non-method item declared in the class.

Defining Methods (Contd.)

Page 14: 03 iec t1_s1_oo_ps_session_04

Slide 14 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Defining a method means declaring the elements of its structure.

Consider the syntax of defining a method:<Access specifier> <Return Type>

<Method Name>(Parameter List)

{

Method Body

}

This 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.

Defining Methods (Contd.)

Page 15: 03 iec t1_s1_oo_ps_session_04

Slide 15 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Defining a method means declaring the elements of its structure.

Consider the syntax of defining a method:<Access specifier> <Return Type>

<Method Name>(Parameter List)

{

Method Body

}

This contains the set of instructionsneeded to completethe required activity.

Defining Methods (Contd.)

Page 16: 03 iec t1_s1_oo_ps_session_04

Slide 16 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

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();

Calling Methods

Page 17: 03 iec t1_s1_oo_ps_session_04

Slide 17 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

The following is an example of calling methods:using System;

class Calculator

{

public int AddNumber(int num1, int num2)

{

int result;

result = num1 + num2;

return result;

}

static void Main(string[] args)

Calling Methods (Contd.)

Page 18: 03 iec t1_s1_oo_ps_session_04

Slide 18 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

{

Calculator cal = new Calculator();

// The following statement is calling the

// AddNumber method and passing 10 and

// 20 as the parameter list.

int value=cal.AddNumber(10, 20);

Console.WriteLine("The result is {0}", value);

Console.ReadLine();

}

}

Calling Methods (Contd.)

Page 19: 03 iec t1_s1_oo_ps_session_04

Slide 19 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

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:

Value

Reference

Output

Using Methods with Parameters

Page 20: 03 iec t1_s1_oo_ps_session_04

Slide 20 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

• Value: The parameters passed by value creates a separate copy in the memory. The following example shows the parameters passed by value:void CalculateSum( int num1, int num2)

{

//…

}

void Accept()

{

int val1=10;

int val2=2;

CalculateSum(val1,val2);

}

Using Methods with Parameters (Contd.)

Page 21: 03 iec t1_s1_oo_ps_session_04

Slide 21 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

• 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 CalculateSum( ref int num1,ref int num2)

{

//…

}

void Accept()

{

int val1=10;

int val2=2;

CalculateSum( ref val1,ref val2);

}

Using Methods with Parameters (Contd.)

Page 22: 03 iec t1_s1_oo_ps_session_04

Slide 22 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

• Output: The output parameters are used to pass the value out of the method. The following example shows the parameters passed by reference:void CalculateSum( ref int num1,ref int num2, out int result){

result=num1+num2;}void Accept(){

int val1=10;int val2=2;int recieveVal;CalculateSum( ref val1,ref val2,out

recieveVal);}

Using Methods with Parameters (Contd.)

Page 23: 03 iec t1_s1_oo_ps_session_04

Slide 23 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Problem Statement:Write a program to swap two numbers by using reference type parameters in a method.

Demo: Swapping Two Numbers by Using Methods with Parameters

Page 24: 03 iec t1_s1_oo_ps_session_04

Slide 24 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

In this session, you learned that:Abstraction is the process of reducing information content in order to retain only the relevant information for a particular purpose.

Encapsulation is the process of hiding all the details of an object that do not contribute to its essential characteristics.

An access specifier is used to determine whether any other class or function can access the member variables and functions of a particular class.

The public access specifier allows a class to expose its member variables and member functions to other functions and objects.

The private access specifier allows a class to hide its member variables and member functions from other class functions and objects.

Summary

Page 25: 03 iec t1_s1_oo_ps_session_04

Slide 25 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

The protected access specifier allows a class to hide its member variables and member functions from other class objects and functions, just like the private access specifier while implementing inheritance.

A method is a set of one or more program statements that can be executed by referring to the method name.

Defining a method means declaring the elements of its structure.

The access modifiers that can be used with methods are public, protected, internal, protected internal, and private.

Parameters allow information to be passed into and out of a method. When you define a method, you can include a list of parameters in parentheses.

Summary (Contd.)

Page 26: 03 iec t1_s1_oo_ps_session_04

Slide 26 of 26Session 4Ver. 1.0

Object-Oriented Programming Using C#

Parameters can be passed by using any one of the following parameters types:

• Value• Reference• Output

– Pass by value is the default mechanism for passing parameters in C#.

– A reference parameter is a reference to a memory location of a data member.

– Output parameters are like reference parameters, except that they transfer data out of the method rather than into it.

– The return statement is used to return the control to the caller.

Summary (Contd.)