Object oriented programming With C#

21
Object-Oriented Programming Youssef Mohammed Abohaty Vice Head Technical at Microsoft Tech Club

Transcript of Object oriented programming With C#

Object-Oriented Programming

Youssef Mohammed Abohaty Vice Head Technical at Microsoft Tech Club

Agenda : -

• Introduction To OOP .

•Classes .

• Enumeration .

•Structures .

• Encapsulation .

• Inheritance .

•Polymorphism .

Introduction To OOP

Traditional Programming = Spaghetti code

!OOP Programming (OOP Arch Code)

Name Space

Class

Method

Code

Why OOP ?

• More Organized Code .

• Debug Fast .

• Fast to Find Errors .

• More Flexible To use .

Classes : - • It’s A container to A Collection of Methods . • It’s Reference Type . • Constructors “is a Method With The Same Name Of

The Class” . • Destructors “is a Method With The Same Name Of The

Class With ~ Before the Name”.

Ex :

Class Persone

{

Public Persone()

{

//Code

}

~ Persone()

{

//Code

}

}

Static And Non Static Class

• Non Static Class To Us It Must Make Instance .

Ex :

Person p = new Person();

p.Speak();

• Static Class You Can Us It Direct .

Ex :

Console.WriteLine(“Hello MVA”);

• How To Make A static Class .

Ex :

Static Class Person

{

Private Void Speak()

{

}

}

OverLoading : -

• Over Loading Is A Group Of Methods Have The

Same Name !

Ex :

Public Void Speak (int Num);

Public Void Speak (int Num ,String Txt);

Ex :

MessageBox.Show(String Text);

MessageBox.Show(String Text,String Caption);

MessageBox.Show(String Text , String Caption , Message BoxButtons Button);

Access Modifier : -

• Public : “ Calling From Any Place “ .

• Private : “ Only Calling From The Same Class “ .

• Protected : “ Only Calling From The Inherit Class “ .

• Internal : “ Only Calling From The Current

Assembly File Only “ .

• Protected Intenal : “ Can Calling From The Inherit

Class And From The Current Assembly File “ .

Enumeration : -

• It’s a Group Of Element’s To Represent Cases and

Different Values Inside The Program .

EX :

enum Human

{

Male=0;

Female=1;

}

• Using Name Of enum Only But Real Value stored

as a byte So We Can Use It at (Windows API) .

Structures : - • Is a special Types To Upload any Properties and

Behaviors as variables Or Methods .

Stuct Human

{

Public String JobTitle;

Public int Age;

Public int Year;

};

Human Mahmoud =new Human();

Mahmoud.JobTitle=“Developer”;

Mahmoud.Age=22;

Mahmoud.Year=2015;

• Struct Can Have an Item Is Another Struct .

• Struct Is a Value Type .

Encapsulation : - • Is Hide Our Code And Use It As a Black Box From

The Final Method .

• using Access Modifier .

EX :

Human Ahmed =new Ahmed();

Ahmed.Age=6723896349;

• We Can Use Get(Accessor) And Set (Mutator). EX :

Person

{

Private int Age;

Public Get Age (int age)

{

If(age <=100 $$ age>=1)

{

Age=age; }

else{console.WriteLine(“We Can’t store This Age”);} }

}

Encapsulation (Type Property) : -

Class Person

{

Public int Age

{

get{return Age ; }

set{if (Age<=100 $$ Age>1)

Age=Value;}

}

}

Inheritance : -

• What Is Inheritance Mean ?! .

• Is-a Relation .

• Has-a Relation .

• When I can Use Inheritance ?! .

• Multi Inheritance ! .

EX :

Class Developer : Human

{

// Implementation

}

Inheritance (Saled“Not Inheritable”) : -

Saled Class Human

{

}

This Way Is Wrong !

Class Teacher : Human ×

{}

• You Can Only Use From Object’s .

• Human Teacher =new Human();

Abstract Classes (MustInherit) :-

• Is A class Must Inherit To Use his attributes From

The Inherited Class . EX :

abstract class Human

{

Public int Age;

}

EX :

class Doctor :Hman

{

}

Interface : -

• Is abstract Class With Out Any Implementation .

Interface Human

{

int Age ;

String JobTitle;

int Year;

}

• More Than Once Inherit .

Polymorphism : -

• You Can Create Class With Out Any

Implementation .

• You Can Change Methods Implementation by

Overriding between two Different Classes.

Public Virtual void Speak()

{

Console,Beep();

}

Public Override void Speak()

{

Console.WriteLine(“Speak”);

}

Abstract Methods : -

• Is a Kind Of Polymorphism but abstract Key Word

Forcing The Developer To Make Override .

Public abstract void Speak()

{

Console,Beep();

}

Public Override void Speak()

{

Console.WriteLine(“Speak”);

}

Thank You !

[email protected]

[email protected]

[email protected]

twitter.com/Dev_Youssef