In general, a method is block of code that helps in performing a functionality. Basically a method looks as follows:
[modifier] Method_name(Parameters of method){....}
The modifier can be either an access sepecifier (like public , private etc.) or keywords such as static. The return type may be a value type or a reference type. The parameters are the variables whose scope is local with respect to the given method. These parameters add dynamic functionality to the code written under the method. The methods under the class may be static or non-static.
Eg: class Hello
{
public int Met(int a,int b)
{
return a*b;
}
public static string Met1(string s)
{
return "Hello"+s;
}
static void Main()
{
Hello h=new Hello();
Console.WriteLine(h.Met(10,2));
Console.WriteLine(Met1("C#"));
}
}
2/08/2010 01:53:00 PM |
Category:
OOP in C#
|
In general sense 'abstraction' mainly means presenting complex scenarios in a simpler fashion. And this could be achieved by creating some simpler interfaces to the complex scenarios. By creating such interfaces it is quiet evident that we trying to hide the complexity of the complex scenarios using the concept of abstraction.
In Object Oriented programming this concept of abstraction is implemented using methods that we create under a class. For example, consider a simple sort() of the Array class under System namespace. To sort a list of array elements we just need to call the sort() using the Array class.(Array.sort(arr_name)). By writing a single line of code we are able to perform complex functionality of sorting. But under the definition of sort() complex sorting algorithms are used which are hidden from the programmer. So the entire complex code for sort() is abstracted and is presented just as a simple method and this simple sort() becomes an interface for the user to perform sorting of array elements.
Eg: class Abs
{
public static void gcd(int a , int b)
{
if(a
{
int c=b%a;
if(c==0)
{
console.WriteLine(a); return;
}
int d=a%c;
if(d==0)
{
console.WriteLine(c); return;
}
...................;
}
}}
In the above class 'Abs', we defined a static method gcd() which performs gcd of 2 numbers. Writing this method is complex and time consuming for the programmer. To avoid this, we can allow programmer to directly call the gcd() under any class and use it-shown as follows:
class Test
{
static void Main()
{
Abs.gcd(3,4);
}
}
So we are abstacting the method gcd() from the programmer. That means we are allowing him to directly consume a method without showing him the definition for that method.
2/05/2010 05:08:00 PM |
Category:
OOP in C#
|
The members of the class (methods, properties, variables etc.) may be classified into two types: static and non-static members. There should be some means to access the class members to perform a functionality.
To access the non-static (or instance) members of the class within the same class or outside the class, we need to take permission from that class. To take permission from the class we need to create a copy of that class which is mostly referred as an object.
Eg: class Test1
{
public int a=10; //non-static member
static void Main()
{
Test1 t1=new Test1();
Console.WriteLine(t1.a); //To access non-static member 'a' within same class
// 'Test1' where it is defined requires object 't1'
//of that class.
}
}
Eg: class Test2
{
static void Main()
{
Test1 t1=new Test1();
Console.WriteLine(t1.a); //To access non-static member 'a' of class 'Test1' from class
//Test2 also requires object 't1' of the class 'Test1'.
}
}
To access the static members of the class within the same class, we can directly refer them. But to access the static members outside the class we need to use that class name under which they are defined to refer them.
Eg: class Test3
{
public static int b=20; //static member
static void Main()
{
Console.WriteLine(b);// static member 'b' defined under class 'Test3' is directly
// referred, no object of 'Test3' required.
}
}
Eg: class Test4
{
static void Main()
{
Console.WriteLine(Test3.b); //static member 'b' of class 'Test3' is consumed under class
// 'Test4' by referring it with class name 'Test3' where it is
//defined
}
}
2/04/2010 02:20:00 PM |
Category:
OOP in C#
|