Top Banner
WELCOME TO OBJECT ORIENTED PROGRAMMING WITH C#.NET
33

Features of Object Oriented Programming: A class is a collection of things which posses common similarities. In C#.NET a class is a user defined.

Jan 02, 2016

Download

Documents

Franklin Terry
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

WELCOMETO

OBJECT ORIENTED PROGRAMMING WITH C#.NET

Page 2: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Features of Object Oriented Programming:

Any programming language to become as object oriented should follow the following features.

AbstractionEncapsulationInheritancePolymorphism

Page 3: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

What is an Abstraction?

It’s a process of Hiding the Implementation but providing the service. There are two types of abstraction available: 1. Data Abstraction2. Functional Abstraction

It is a process of Binding the member variable of a class along with member functions.Encapsulation can be implemented with the help of object and also Access modifiers like Private ,public and Protected etc.

What is an Encapsulation?

Page 4: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

What is an Inheritance?

This is process of creating a new class from already existing classIn inheritance process existing class is known as Parent/Base class, newly created class is know as Derived/Child class.In inheritance process, child class will get all the features of Parent/Base class.Main purpose of inheritance is code Re-usability and providing additional functionality/enhancement.

Page 5: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

What is Polymorphism?

It’s derived from a Greek word, where poly means many morph means Faces/Behaviors.Types of Polymorphism:There are two types of polymorphism1. Static polymorphism/Compile Time polymorphism /Early

Binding2. Dynamic polymorphism/Run Time polymorphism/late

Binding Static polymorphism is achieved using, Function over

loading, operator over loading. Dynamic polymorphism is achieved by using , Function

overriding.

Page 6: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Class in C# .NET

A class is a collection of things which posses common similarities.

In C#.NET a class is a user defined Data type and is Known as Reference Type.

A class can contain following members in C#.NET

1. Data Fields2. Function3. Constructors4. Destructors5. Properties6. Indexers7. Events

Page 7: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

1.Data Fields

It’s used to store the data related to the class. Except Data Fields no other member of a class can store the data.To declare any Data Field, we use the following

Syntax

Access modifier Data type Data Field Name = [Initializing value];

Ex:Public int Empid;

Page 8: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

2.FUNCTIONS

Any programming language will have methods.

A named block of code is refer as a Method or subprogram.

It can be either value returning or non value returning.

Syntax[<modifiers>]<type|void><name>([<pram

def’s])

{

statements;

};

Page 9: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Syntax to create a class

Access Modifier class Class Name

Syntax to create an Object

Class Name Object Name=new Class Name([args list])

Constructor name

Ex: Program p= new program() orProgram p;P=new program();

Page 10: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Constructors

A constructor is a member method of a class which is invoked automatically when an object to the class is created.A constructor name should be same name as class name .A constructor does not have any return type even void also.A constructor is used to keep something ready for the object when it is created .

Syntax[<modifiers>]<Name>([<Param Def’s>]){Statements;}

Page 11: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Constructors are TWO Types

1

2

Default Constructor

Parameterized Constructor

A constructor without any parameters is a “Default constructor” which can be defined either by the programmer or will be defined by the compiler provided the class doesn't contain any constructor in it.A constructor with parameters is referred as “parameterized constructor” which can be defined only by a parameters.If at all constructors are parameterized we can send values to the parameters at the time of object creation.

Page 12: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Example program

using System;using System.Collections.Generic;using System.Linq;using System.Text;

namespace ClsEmployee1{ class Program { int eid, eage; string ename, eaddress; public Program() { this.eid = 101; this.ename = "sai"; this.eaddress = "mcl"; this.eage = 24; }

public void DisplayEmpData() { Console.WriteLine("eida is "+eid); Console.WriteLine("ename is"+ename); Console.WriteLine("eaddress is " +eaddress); Console.WriteLine("eage is" + eage); } class ClsUDConst { static void Main(string[] args) { Program obj1 = new Program(); Program obj2 = new Program();

obj1.DisplayEmpData(); obj2.DisplayEmpData(); Console.Read(); } } }}

Page 13: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

INHERITANCE

The process creating a new class form already existing class.Existing class is known as Parent Class or Base Class.New class is known as Child Class or Derived Class.

Child class will get all the features of parent classCode Reusability and providing additional functionalities

consume

members

class1

Parent child Relation

Page 14: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Syntax

[<Modifiers>] class <C C Name>:<P C Name>

Class Class1{ Members}Class Class2 : Class1{ Consume the parent members here}

Child ClassParent Class

Page 15: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Example program

Add a class class1.cs and write the following code

Class class1{Public class1 (){Console.WriteLine (“class1 constructor”);}Public void Text 1(){Console.writeLine (“Method one”);}Public void Text2(){Console.WriteLine (“Method Two”);}}

Add a class class2.cs and write the following code

Class class2:class1{Public class2 (){Console.writeLine (“class 2 constructor”);}Public void Text3 (){Console.writeLine (“Method Three”);}Static VoidMain (){Class2 c=new class2 ();c.Text1 ();c.Text12 ();c.Text3 ();console.Readline ();}}

Page 16: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Polymorphism

Polymorphism can be implemented with the help of TWO approaches

Over Loading

Overriding

Same name different signatures are called overloading method

Same name and same signature is called overriding method

Page 17: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Example program

Add a class Load demo.cs and write the following code

Class LoadDemo{Public void show (){Console.WriteLine (1);}Public void show (int x){Console.WriteLine (2);}Public void show (string s){Console.WriteLine (3);}

Public void show (int x, string s){Console.WriteLine (4);}Public void show (string s, int x){Console.WriteLine (5);}Static VoidMain (){Load demo obj =new LoadDemo ( );Obj. Show (10);Obj. show (“Hello”);Obj. Show (10,”Hello”);Obj. Show (“Hello”, 10);Console.Readline ();}}}

Page 18: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Properties

A property is a member of a class , used to write the data in the data field and read the data from the data field of a class.A property can never store the data, just used to transfer the data.Properties are members that provide a flexible mechanism to read, write or compute the values of private fields.Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.To perform read and write operations, property can contain two assessors/methods.

Page 19: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Assessors with in the property

Accessor enable data to be accessed easily while still providing the safety and flexibility of methods.

Set Accessor

Get Accessor

Page 20: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Set Accessor

Set accessor is used to write the data into the data field.This will contain a default and fixed variable named as “value”.Whenever we call the property to write the data, any data we supply will come and store in value variable by default.

Set{Data File Name=value;}

Get AccessorGet Accessor is used to read the data field, using this accessor we can’t write the data.

Get{Return Data File Name:}

Syntax

Page 21: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Types of properties

C#.NET will support three types of Properties.

1

•Read Only Property

2

•Write Only Property

3

•Read Write property

This property is used to read the data from the Data field

It is used to write the data into Data field of a class.

It is used to read the data from the Data field and to write the data in to the Data Field

Access Modifier Data Type property Name{ set { Data Field Name=Value; }}

Access Modifier Data Type Property Name{ get { return data Field Name; }}

Page 22: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Access Modifier Data Type property Name{ set { Data Field Name=Value; } get { return data Field Name; }}

Namespace CAProperties{Class clsEmpolyee{ int EmpId,EAge; Public int PEmpId { set { EmpId=value; } get { return EmpId; } }

Syntax Example

Page 23: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Public int PEAge {

set { EAge=Value; } get { return EAge; }

}}Class ClsProperty1{ Static void Main(string[] args) { clsEmployee obj1=new clsEmployee(); Console.write(“Enter Employee details:”); Obj1.PEmpId=Concert.ToInt32(Console.ReadLine()); Obj1.PEAge=convert.ToInt32(console.ReadLine()); } } }

Page 24: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Abstract

It is a method with can have only syntax, but no implementation is called

Abstract Method.

It should be overridden in the Sub Class.

It can be used mostly in Distributed Applications (Client/sever applications)

AccessSpecifier abstract Return Type

Method Name(parameters);

Syntax

Public abstract void Hello();Example

Page 25: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Interfaces

In the .NET Framework, interfaces have a special naming convention. All interface name begin with a capital I. Interfaces can have properties as we difference from a class.

1. You cannot create an object of an interface2. An interface does not contain any constructor.3. All methods in an interface are abstract.4. An interface can inherit multiple interfaces.

[<modifiers>] interface<name>

{Abstract member declarations

}

Syntax

Page 26: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Add an interface inter1.cs and write the following code

Interface inter1{ Void Add (int x, int y); Void sub (int x, int y); Void test (); }}Add an interface inter2.cs and write the following codeInterface inter2{ Void Mul (int x, int y); Void Div (int x, int y); Void test (); }}

To implement both the above two interfaces Add a class interclass.cs and write the following code

Class Interclass: Inter1, Inter2{ Public void Add (int x, int y) {Console.Writeline(x+y); }Public void Sub (int x, int y) {Console.Writeline(x-y);}Public void Mul (int x, int y) {Console.Writeline(x*y);}

Page 27: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Public void Div (int x, int y) {Console.Writeline(x/y)}public void Test (){Console.Writeline (“Declared under multiple interfaces”);}Void Inter1.Test (){Console.Writeline (“Method of Interfaced 1”);}Void Inter2.Test (){Console.Writeline (“Method of Interfaced 2”);}

Static Void Main (){Inter class obj= new Inter class ();Obj. Add (100, 45);Obj. Sub (100, 45);Obj. Mul (100, 45);Obj.Div (100, 45);Obj. Test ();Inter1 i1 = obj; Inter i2 = obj;I1.Test (); i2.Test ();Console.ReadLine ();}}

 

Page 28: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Delegates

It’s used to represent or refer one or more functions.Delegates are user defined types in C#.NET.Delegates in C#.NET are similar to function pointers in C++.It is not a member of a class, but similar to a class.To consume any delegate , we need to create an object to delegate. Delegate is a type that references a method.

The delegate method can be invoked like any other method with parameters and written value.These can be used to define callback methods.There are the backbone for events.

Page 29: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Types of Delegates

There are of two types of Delegates available.a) Single cast delegateb) Multi cast delegate

A delegate that represents only function is known as Single Cast Delegate.

A delegate that represents only a more than one functions is known as Multi Cast Delegate.

To work with Delegate Use the Following Steps:Creating a DelegateInstantiating a DelegateInvoking a Delegate

Page 30: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

EX: If we consider a function, like Public Void Add (int a , int b ){Code……}To refer this function, if we want to use the Delegate, we use the following steps like…..

STEP 1 Creating A Delegate

Access modifier delegate return type Delegate name([arguments list])EX: public delegate void sampleDelegate(int x, int y)

Syntax

Page 31: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

STEP 2 Instantiating a Delegate

Delegate name object name = new Delegate name (Target function name);

EX: SampleDelegate obj=new SampleDelegate(Add);

STEP 3 Invoking a Delegate

Object Name([Arguments Values])

EX: Obj(10,20)

Syntax

Syntax

Page 32: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Add a class del.cs and write the following the codeClass Delclass{ Public String SayHello(string name0 { Return “Hello” +name; }Public delegate string SayDel(String name);Static void Main() {

Delclass obj=new Delclass();SayDel Sd=new SayDel(obj.sayHellow);Console.WriteLine(Sd(“xxx”));Console.WrireLine(Sd(“yyy”));Console.WrireLine(Sd(“zzz”));Console.ReadLine();

}}

Program

Page 33: Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.

Thank You