Welcome to the amazing dot net programming

Author: Vijaya Kumar
Contact:

    

  

Get updates by e-mail

HP Computer Museum

 

 

 

 

free website submission search engine seo optimization

 

Powered by Blogger

October 07, 2006

OOPs in ASP.NET C#

Here i am adding the definations of class, interface and OOPS concept methodologies so it will be easy to implement the applications.

All the programming languages supporting Object oriented Programming will be supporting these three main concepts:
Encapsulation
Inheritance
Polymorphism

Encapsulation in C#: Encapsulation is process of keeping data and methods together inside objects.

public class Aperture
{
public Aperture()
{

}

protected double height;
protected double width;
protected double thickness;

public double GetVolume()
{
double volume = height*width*thickness;
if(volume<0)
return 0;
return volume;
}
}

In this example we encapsulate some data such as height, width, thickness and method GetVolume. Other methods or objects can interact with this object through methods that have public access modifier. It must be done using ?.? operator.

Inheritance in C#: In a few words, Inheritance is the process of creation new classes from already existing classes. The inheritance feature allows us to reuse some parts of code. So, now we have some derived class that inherits base class?s members. Consider the following code snippet:

public class Door : Aperture
{
public Door() : base()
{

}

public bool isOutside = true;

}

As you see to inherit one class from another, we need to write base class name after ?:? symbol. Next thing that was done in code Door () ? constructor also inherits base class constructor. And at last we add new private field. All members of Aperture class are also in Door class. We can inherit all the members that has access modifier higher than protected.

Polymorphism in C#: Polymorphism is possibility to change behavior with objects depending of object?s data type. In C# polymorphism realizes through the using of keyword virtual and override. Let look on the example of code:

public virtual void Out()
{
Console.WriteLine("Aperture virtual method called");
}
//This method is defined in Aperture class.
public override void Out()
{
Console.WriteLine("Door virtual method called");
}

OOPs with C#.Net

Encapsulation
Abstraction
Inheritance
Polymorphisum

Class and Object


Scope:

Declared accessibility
Meaning
public
Access is not restricted.
protected
Access is limited to the containing class or types derived from the containing class.
internal
Access is limited to the current assembly.
protected internal
Access is limited to the current assembly or types derived from the containing class.
private
Access is limited to the containing type.


Private : with in the class only

Protected : with in the class , with in the derived class

Internal : with in the file ( with in the class, with in the derived , with the object name of base and derived )

Protected Internal : with in the assembly and with in the Derived class

Public : any where

Members of
Default member accessibility
Allowed declared accessibility of the member
enum
public
None
class
private
public
protected
internal
private
protected internal
interface
public
None
struct
private
public
internal
private

Class:

public class MyMath
{
public int Add(int a,int b)
{
return a+b;
}
}

Object :

MyMath m=new MyMath();
Or
MyMath m;
m=new MyMath();

Property Procedure:

int i;
public int propI
{
get
{
return i;
}
set
{
i=value;
}
}

Method Overloading:

public int Add(int a,int b)
{
return a+b;
}

public int Add(int a,int b,int c)
{
return a+b;
}

Constructor:

int i;
public MyMath()
{
i=0;
}
public MyMath(int i)
{
this.i=i;
}

Static Member:

public static int a,b;
static MyMath()
{
a=0;
b=0;
}
public static int display()
{
return a+b;
}

Inheritance: overloading,overriding

public class Emp
{
private int eno;
private string ename;
protected float sal;
protected float bonus;

public Emp(int eno,string ename,float sal)
{
this.eno = eno;
this.ename = ename;
this.sal = sal;
}

public void CalcBonus()
{
bonus = (float) (sal * 0.1);
}

public virtual void display()
{
Console.WriteLine(eno);
Console.WriteLine(ename);
Console.WriteLine(sal);
Console.WriteLine(bonus);
}
}

public class MrktEmp:Emp
{
private float ta;

public MrktEmp(int eno,string ename,float sal): base(eno,ename,sal)
{
ta=1000;
}

public void CalcBonus(float f)
{
bonus = sal * f;
ta= sal * f;
}

public override void display()
{
base.display();
Console.WriteLine(ta);
}
}

calling:

static void Main(string[] args)
{
Emp e=new Emp(10,"sample",10000);
e.CalcBonus();
e.display();
MrktEmp me=new MrktEmp(20,"AAAAA",20000);
me.CalcBonus((float)0.5);
me.display();
Console.Read();
}

Abstract Class:

public abstract class Emp
{
public abstract void display();
}

public class mrktEmp : Emp
{
public override void display()
{
Console.WriteLine("Hi");
}
public string Hello()
{
return "Hello";
}
}

Polymorphisum:

public class Emp
{
public int eno;
public virtual void display()
{
Console.WriteLine(eno);
}
}

public class mrktEmp:Emp
{
public override void display()
{
Console.WriteLine(eno + " in derived");
}
}

calling:

static void Main(string[] args)
{
Emp e=new mrktEmp();
e.display();
Console.Read();
}

Sealed Class:

public sealed class Emp
{
public int eno;
public void display()
{
Console.WriteLine(eno);
}
}

Interface:

interface i
{
void display();
}
public class Emp : i
{
#region i Members
public void display()
{
Console.WriteLine("i");
}
#endregion
}

0 Comments:

Post a Comment

<< Home

Google
 
Web dotnetlibrary.blogspot.com