380 likes | 508 Views
WELCOME. TO. Object oriented programming with C#.NET. Features of Object Oriented Programming:. Any programming language to become as object oriented should follow the following features. Abstraction Encapsulation Inheritance Polymorphism. What is an Abstraction?.
E N D
WELCOME TO Object oriented programming with C#.NET
Features of Object Oriented Programming: Any programming language to become as object oriented should follow the following features. • Abstraction • Encapsulation • Inheritance • Polymorphism
What is an Abstraction? • It’s a process of Hiding the Implementation but providing the service. • There are two types of abstraction available: • Data Abstraction • Functional Abstraction What is an Encapsulation? • 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 Inheritance? • This is process of creating a new class from already existing class • In 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.
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 polymorphism • Static polymorphism/Compile Time polymorphism /Early Binding • 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.
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 Data Fields Function Constructors Destructors Properties Indexers Events
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;
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; };
Syntax to create a class Access Modifier class Class Name Syntax to create an Object Ex: Program p= new program() or Program p; P=new program(); Class Name Object Name=new Class Name([args list]) Constructor name
How to open Microsoft visual studio .NET Open V.S Go to File New project Change the language as C# template as Console application Named return as OOPS Project and the write the following code under the default class program Class program { //No input and out put Void Text1 () { Console.WriteLine (“first method”); }
// No output but has Input Void Text2 (int x, int y) { For (int i=1;i<=y;i++)Console.writeLine (“{0} *{1}={2}”;x, i, x*i); }//No Input but has out put String Text3 (){Return “third method”;}//has input and out putString text4 (string name){return “Hello” +name;}
//Method with Multiple output parameters Void Meth1 (int x, int y, ref int a, ref int b) { a=x+y; b=x/y; } Static VoidMain (string [] args) { Program p=new program (); p.Text1 (); p.Text2 (5, 10); Console.WriteLine (p.Text3 ()); Console.WriteLine (p.Text4 (“venkat”)); Int m=0, n=0; //Initialization is mandatory p.Math1 (100, 50, ref m, ref n); Console.WriteLine (M+” “+n); Int q, r; //Initialization is optional p.Math2 (100, 50, out q, out r); Console.WriteLine (q+” “+r); Console.Readline (); } }
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; }
Example program Add a class ConDemo.cs and write the following code Class ConDemo { ConDemo () { Console.WriteLine (“Constructor”); } Void Demo () { Console.writeLine (“Method”); } Static VoidMain () { ConDemo cd1=new ConDemo (); ConDemo cd2=new ConDemo (); ConDemo cd3=cd2; Cd1.Demo (); cd2.Demo (); cd3.demo (); Console.ReadLine (); } }
Constructors are TWO Types 1 Default Constructor 2 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.
Example program Add a class Maths.cs and write the following code Class Maths { Int x, y; //class variables Maths (int x, int y ) //block variables { This.x=x; This.y=y; } Void Add () { Console.WriteLine(x+y); } Void Sub() Console.WriteLine(x-y); } Void Mul () { Console.WriteLine(x*y); }
Void Div () { Console.WriteLine(x/y); } Static VoidMain () { Maths m=new Maths (100, 50); m.Add (); m.Sub () ; m.Mul(); m.Div(); Console.ReadLine (); } }
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 class • Code Reusability and providing additional functionalities class1 members Parent child Relation consume
Syntax Child Class Parent Class [<Modifiers>] class <C C Name>:<P C Name> Class Class1 { Members } Class Class2 : Class1 { Consume the parent members here }
Example program 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 (); } } 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”); } }
Polymorphism • Over Loading • Polymorphism can be implemented with the help of TWO approaches • Overriding Same name different signatures are called overloading method Same name and same signature is called overriding method
Example program 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 (); } } } 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); }
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.
Assessors with in the property • Accessor enable data to be accessed easily while still providing the safety and flexibility of methods.
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 Accessor Syntax Set { Data File Name=value; } Get { Return Data File Name: } Get Accessor is used to read the data field, using this accessor we can’t write the data. Get Accessor
Types of properties • C#.NET will support three types of Properties. • 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; } }
Syntax Example Namespace CAProperties { Class clsEmpolyee { int EmpId,EAge; Public int PEmpId { set { EmpId=value; } get { return EmpId; } } Access Modifier Data Type property Name { set { Data Field Name=Value; } get { return data Field Name; } }
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()); } } }
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) Syntax AccessSpecifier abstract Return Type Method Name(parameters); Public abstract void Hello(); Example
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. • You cannot create an object of an interface • An interface does not contain any constructor. • All methods in an interface are abstract. • An interface can inherit multiple interfaces. [<modifiers>] interface<name> { Abstract member declarations } Syntax
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 code Interface 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); }
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 (); } }
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.
Types of Delegates • There are of two types of Delegates available. • Single cast delegate • 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 Delegate • Instantiating a Delegate • Invoking a Delegate
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 Syntax Access modifier delegate return type Delegate name([arguments list]) EX: public delegate void sampleDelegate(int x, int y)
Instantiating a Delegate STEP 2 Delegate name object name = new Delegate name (Target function name); Syntax EX: SampleDelegate obj=new SampleDelegate(Add); Invoking a Delegate STEP 3 Syntax Object Name([Arguments Values]) EX: Obj(10,20)
Add a class del.cs and write the following the code Class Delclass { Public String SayHello(string name0 { Return “Hello” +name; } Public delegate string SayDel(String name); Static void Main() { Delclass obj=new Delclass(); SayDelSd=new SayDel(obj.sayHellow); Console.WriteLine(Sd(“xxx”)); Console.WrireLine(Sd(“yyy”)); Console.WrireLine(Sd(“zzz”)); Console.ReadLine(); } } Program