Monday, June 29, 2009

OOPS Introduction

Introduction
.NET is fully object oriented platform that allow languages to take full advantage of these OO features. The features include:
Namespace
Classes
Objects
Encapsulation
Overloading
Inheritance
Overriding
Interfaces
Polymorphism
Abstraction
Let's take a quick look at what each of this term means.

Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.
Namespace
Even though namespace is not really an OOPs feature, .NET use it extensively. Namespace is nothing but a

logical container for various classes. Under given namespace class names must be unique. Namespace server two purposes - they provide logical organization of classes and they also avoid ambiguity.
Classes
Class is nothing but a template or blue-print for an entity. For example you may have a class that represents real life entity - Employee. The class will provide properties (Name, Age...) as well as actions (CalculateSalary, GoOnLeave...) of the entity.`

, a class is a template definition of the methods and variables in a particular kind of object . Thus, an object is a specific instance of a class; it contains real values instead of variables.
Objects
Instances of classes are called as objects. For example there might be three instances of the Employee class mentioned above. They might represent individual employees - John, Bob and Tom.

An object doesn't exist until an instance of the class has been created; the class is just a definition. When the object is physically created, space for that object is allocated in RAM. It is possible to have multiple objects created from one class.

Encapsulation
Each object typically deals with some kind of data or the other. Not all the data needs to be exposed to external systems. This can be controlled via data encapsulation.
Overloading
Overloading refers to the methods or functions having same name but varying parameters. The parameters should vary with respect to data types and order.
Inheritance
Inheritance refers to extending functionality of existing class. Inheritance is useful when developing "object models" for your system. .NET supports only single inheritance.
Overriding
Overriding refers to the methods in the child class having the same signature (name as well as parameters) as of the parent class methods.
Interfaces
Interfaces are models of class properties and methods without any implementation. The class implements the interface. Once a class implements any interface it must implement all the properties and methods (although the implementation can be empty or null implementation).

All interfaces should be declared with the keyword interface. You can implement any number of interfaces in a single derived class, but you should provide signatures to all method definitions of the corresponding interfaces.
Polymorphism
Polymorphism refers to the ability of the system to call correct implementation of methods with the same name. For example, Clerk as well as Manager class might have a method called CalculateSalary(). However, at runtime depending on whether the underlying object is of type Clerk or Manager correct version of the method is called.

Structure

 Definition  Normally, a structure is a light-weight class used to create user-defined types containing only public fields and no properties. A structure is considered a value type. .NET structures support access modifiers, constructors, indexers, methods, nested types, operators, and properties. Unlike classes, structures do not support custom constructors, destructors, inheritance, or compile-time initialization of instance fields. Performance suffers when using structures in situations where reference types are expected due to Boxing and Unboxing.
Creating classes
Creating classes is similar to creating namespaces.
[VB.NET]
Public Class Class1
...
End Class

[C#]
public class Class1
{
...
}
Generally classes will be part of some of the namespace.
Creating Properties
Properties encapsulate data members of your class. Properties can be read-write, read only or write only. Here is how you create read-write properties:
[VB.NET]
Public Class Employee

private strName As String

Public Property Name As String
Get
return strName;
End Get

Set(value As String)
strName=value;
End Set
End Property
End Class

[C#]
public class Class1
{
public string Name
{
string strName;
get
{
return strName;
}
set
{
strName=value;
}
}
}
Here,
VB.NET uses Property keyword to declare properties. C# does not have this keyword
Property definition consists of two parts Get and Set. The get part returns the property value and set par sets some private variable.
The value in Set routine is received via implicit variable called value in C#. VB.NET allows you to change this.
Creating methods
Methods represent actions performed by the object. In VB.NET functions and sub routines are collectively called as methods. In C# everything is function.
[VB.NET]
Public Sub CalculateSalary()
...
End Sub

[C#]
public void CalculateSalary()
{
...
}
Method overloading
Method overloading refers to methods with same name but different types or order of parameters. Following example make it clear:
[VB.NET]
Public Sub CalculateSalary()
...
End Sub

Public Sub CalculateSalary(month as Integer)
...
End Sub

[C#]
public void CalculateSalary()
{
...
}

public void CalculateSalary(int month)
{
...
}
In VB.NET you can also use optional parameters to achieve similar functionality. However, it is recommended to use overloading instead to make your code consistent across languages.
Inheritance
Inheritance is the ability to extend existing class further. Unlike languages like C++ that allow multiple inheritance .NET allows only single inheritance. This means that at a time you can inherit from a single class.
[VB.NET]
Public Class Manager
Inherits Employee
...
End Class

[C#]
public class Manager : Employee
{
...
}
In the above example, we create a class called Manager that inherits from Employee class. As you can guess Manager is specific implementation of generic Employee class. VB.NET uses Inherits keyword to indicate the parent class where as C# uses : operator to indicate that.
Method Overriding
In order to override method in child class they need to be marked as Overridable (VB.NET) or virtual (C#) in the parent class.
[VB.NET]
Public Overridable Function CalculateSalary() As Integer
...
End Function

[C#]
public virtual int CalculateSalary()
{
...
}
Then in the child class you can create a method with the same signature and specify that it overrides the base class method.
[VB.NET]
Public Overrides Function CalculateSalary() As Integer
...
End Function

[C#]
public override int CalculateSalary()
{
...
}

Note that if you do not provide the Overrides (VB.NET) or override (C#) keywords in the child class the compiler issues a warning that you are hiding a base class member. In this case you can either put the above keywords or use Shadows (VB.NET) or new (C#) keywords. Using these keywords ,however, will hide the base class members.
Creating Interfaces
Just like classes are templates for real life entities, interfaces can be thought of as templates for classes. They bring uniformity in your object model.
[VB.NET]
Public Interface IEmployee
Property EmployeeID() As Integer
Property Name() As String
Property Age() As Integer
Function CalculateSalary() As Integer
End Interface

[C#]
public interface IEmployee
{
int EmployeeID
{
get;
}

string Name
{
get;
set;
}

int Age
{
get;
set;
}

int CalculateSalary();
}
As you can see VB.NET uses Interface keyword to define an interface. Similarly, C# uses interface keyword. Note, how they contain only property and method signatures and no code at all.
Implementing Interfaces
The main difference between inheritance based programming and interfaces based programming is that - interfaces just specify signatures of properties and methods for a class. Your class "implements" the interface by providing implementation for various properties and methods. Unlike inheritance there is no "code" inherited from interfaces. Your class can implement one or more interfaces.
[VB.NET]
Public Class Manager
Implements IEmployee
...
Public Function CalculateSalary()
As Integer Implements IEmployee.CalculateSalary
...
End Function
End Class

[C#]
public class Manager : IEmployee
{
...
public int CalculateSalary()
{
...
}

}
Above example shows how VB.NET uses Implements keyword to implement an interface. Note how VB.NET also requires the use of Implements keyword for each property and method. You must have guessed from this that in VB.NET you can give different name to the implemented member than the interface. This feature is not available in C#. C# do not have a special keyword and uses the same : operator to implement the interface.

Abstract Classes

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.
Polymorphism
Consider following lines of code:
[VB.NET]
Dim emp As Employee
emp = New Clerk()
Console.WriteLine
("Clerk Salary :{0}", emp.CalculateSalary())
emp = New Manager()
Console.WriteLine
("Manager Salary :{0}", emp.CalculateSalary())

[C#]
Employee emp;
emp=new Clerk();
Console.WriteLine
("Clerk Salary :{0}",emp.CalculateSalary());
emp=new Manager();
Console.WriteLine
("Manager Salary :{0}",emp.CalculateSalary());
Here, we have declared a variable of type Employee. A variable of parent class type can point to instance of any of its children. First, we point it to an instance of Clerk class. Then we point it to an instance of Manager class. Even though the variable is of type Employee, depending on which child type it is pointing to it calls the correct implementation of CalculateSalary() method. The underlying system does this via inheritance polymorphism. Similar thing can also be achieved in interface polymorphism.
[VB.NET]
Dim emp As IEmployee
emp = New Clerk()
Console.WriteLine
("Clerk Salary :{0}", emp.CalculateSalary())
emp = New Manager()
Console.WriteLine
("Manager Salary :{0}", emp.CalculateSalary())

[C#]
IEmployee emp;
emp=new Clerk();
Console.WriteLine
("Clerk Salary :{0}",emp.CalculateSalary());
emp=new Manager();
Console.WriteLine
("Manager Salary :{0}",emp.CalculateSalary());
Summary
In this article we saw many OO features of VB.NET and C#. You must have got idea how VB.NET as well as C# provide closely matching features and keywords in this area

C# - Language


Access Modifiers:

Public: Allows access to the class member from any other class.
Private: Allows access to the class member only in the same class.
Protected: Allows access to the class member only within the same class and from inherited classes.
Internal: Allows access to the class member only in the same assembly.
Protected internal: Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly.
Static: Indicates that the member can be called without first instantiating the class.

Parameters:

Ref and out Parameters:
When we pass a parameter as ref to a method, the method refers to the same variable and changes made will affect the actual variable.Even the variable passed as out parameter is similar to ref, but there are few implementation differences when you use it in C#.
Argument passed as ref must be initialized before it is passed to the method, where as in case of  out its is not necessary,but after a call to the method as an out parameter the variable must be initialized
When to use ref and out parameter. out parameter can be used when we want to return more than one value from a method
Interfaces:
This C# Tutorial deals with interfaces in C# .Net. An Interface is a reference type and it contains only abstract members. Interface's members can be Events, Methods, Properties and Indexers. But the interface contains only declaration for its members. Any implementation must be placed in class that realizes them. The interface can't contain constants, data fields, constructors, destructors and static members. All the member declarations inside interface are implicitly public
public interface IDrivable
{
  void GoForward(int Speed);
}
public class Truck : IDrivable
{
  public void GoForward(int Speed)
  {
     // Implementation omitted
  }
}
public class Aircraft : IDrivable
{
  public void GoForward(int Speed)
  {
     // Implementation omitted
  }
}
public class Train : IDrivable
{
  public void GoForward(int Speed)
  {
     // Implementation omitted
  }
}

Detail Url : http://www.codersource.net/csharp_tutorial_interface.html

Delegates and Events

A Delegate holds reference/s of one or more functions, which can be called or invoked as and when needed.Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.
An interesting and useful property of a delegate is that it does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.
http://www.akadia.com/services/dotnet_delegates_and_events.html

Structs:

In C#, structs are value types, classes are reference types. There are two ways you can create value types, in C#, using the enum keyword and the struct keyword. Using a value type instead of a reference type will result in fewer objects on the managed heap, which results in lesser load on the garbage collector (GC), less frequent GC cycles, and consequently better performance.
structs cannot derive from any other class/struct, nor can they be derived from. However, a struct can implement any number of interfaces. Be aware, though, that when you treat the struct as an interface, it gets implicitly boxed, as interfaces operate only on reference types.
Detail Url: http://www.codeproject.com/csharp/structs_in_csharp.asp
Detail Url : http://www.c-sharpcorner.com/Language/StructuresInCSRVS.asp

Abstract classes
abstract class has methods that are NOT implemented, but just declared, the deriving class should implement them.
The classes, which we can't initialize, are known as abstract classes. They provide only partial implementations. But another class can inherit from an abstract class and can create their instances.
An abstract class can contain abstract and non-abstract methods. When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class.
An abstract method is a method without any method body. They are implicitly virtual in C#.
DetailUrl:http://www.csharpcorner.com/Language/AbstractClassesNMethodsRVS.asp
Method Overriding
Overriding refers to the methods in the child class having the same signature (name as well as parameters) as of the parent class methods
Note that for one method to override another, the overridden method must not be static, and it must be declared as either 'virtual', 'abstract' or 'override'. Furthermore, the access modifiers for each method must be the same.

Enums

A enumeration is a distinct type that consists of a set of named constants called the enumetor list. Every enumeration type has an underlying type which can be all integral times except that of a char type.

41 comments:

rufinadjenning0820 said...

免費成人視訊.av美美色網影片.台中援交友留言成人 視訊.北台灣視訊http sex520 net.jp成人.視訊聊天室捷克論壇.一夜情台北視訊.sex520 net視訊美女.情色視訊交友壞朋友論壇.69性殿.aaa免費看影片.s383情色大網咖視訊美眉共和國.aio交友愛情館視訊交友90739.影音視訊聊天室dudu sex.sexy girl video movie show-live視訊.杜雷斯影城.一夜情視訊網際論壇.pchome交友免費視訊美女.壞朋友論壇一夜情視訊.情色視訊禁地論壇.aio一夜情視訊網愛聊天室.520sex成人視訊.米克綜合論壇視訊 美女.浪漫月光論壇成人視訊.aio免費視訊辣妹.34c南台灣視訊情人視訊網.視訊美女sogo 色論壇.s383情色大網咖情人視訊網.s383情色視訊網.援交友聊天室視訊美女館.34c美女寶貝視訊.一對一視訊辣妹妹影音視訊聊天室.一夜情辣妹no36k情人視訊網.辣妹有約辣妹no31314視訊.情色視訊做愛.gogobox論壇免費視訊聊天室.聊天室ukiss.拓網學生族視訊777美女.視訊聊天室777美女.學生族視訊-性趣十足.免費視訊聊天室捷克論壇.免費視訊聊天室-sex520.大眾論壇視訊 美女.拓網免費視訊辣妹脫衣秀. 視訊做愛.sex520 net南台灣視訊.sex520 net視訊.北台灣視訊援交聊天室.18成人視訊網.s383情色視訊大網咖.拓網天空交友高雄視訊.aaa片免費看影片.做愛視訊聊天室.高雄視訊交友.yam交友辣妹妹影音視訊聊天室.鹹濕視訊米克綜合論壇.壞朋友論壇免費視訊.美眉共和國080情人網.美眉共和國.臺灣情色網視訊交友.臺灣情色網hilive tv.玩美女人影音秀2sex999情人輔助品

chetcsturgil said...

美女遊戲,情色a片,成人遊戲,上班族聊天室,巨乳,成人論壇,嘟嘟貼圖區,美女寫真,ut聊天室,同志聊天室,色情小遊戲,貼圖區,哈比寬頻成人,嘟嘟成人,玩美女人,視訊交友,自拍貼圖,正妹計時器,919sex成人網,免費a片觀看,jp成人,正妹牆,聊天室,ut聊天室,視訊聊天室,情色,微風成人,豆豆聊天室,視訊美女,85cc成人片,85cc成人片觀看,交友戀愛進行室,嘟嘟成人網,成人,色情,美女,色情小說,情色貼圖,情色小說,交友覓戀會館,情色文學,

antoniasrego said...

美女貼圖偷拍貼圖台中寫真棉花田本土美女寫真影片免費觀賞無碼光碟免費交友go交友覓戀會館正妹交友goking7777net免費交友ggo台灣18成人新竹聊天室網愛聊天室寫真集影片免費寫真女郎影片情色典獄長自拍照918色情故事

開會討論 said...

Unable to give you a heart. so have a reply to push up your post. ........................................

淑俊銘松 said...

Unable to give you a heart. so have a reply to push up your post. ........................................

王俊貴 said...

Look before you leap.......................................................

BlancaMcleroy1230 said...

how do u do?xvideo打飛機專用網洪爺免費洪爺色情片洪爺貼圖區洪爺成人線上洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼援交妹辣妹野球拳情色文學情趣聊天室性感辣妹裸體遊戲做愛偷拍一夜情視訊洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺色情貼洪爺免費色情洪爺成年人網洪爺貼圖洪爺色論壇洪爺影城洪爺成人線上洪爺貼圖區洪爺色情片洪爺免費洪爺免費洪爺色情片洪爺貼圖區洪爺影城洪爺色論壇洪爺貼圖洪爺成年人網洪爺免費色情洪爺色情貼洪爺成人線上

興偉 said...

^^ 謝謝你的分享,祝你生活永遠多彩多姿!........................................

俊淑娟美 said...

錢,給你帶來歡愉的日子,但不給你帶來和平與幸福........................................

淑合 said...

A good medicine tastes bitter.

智能 said...

Just do it!.......................................................

函松 said...

不要把生命看得太嚴肅,反正我們不會活著離開。..................................................

文佩齊華 said...

真是好文呀~~給你拍拍手再加分!!!..................................................

皓珊皓珊 said...

your english is incredible............................................................

歐英傑 said...

you‘re so smart!............................................................

怡潔怡潔 said...

The absent are always in the wrong. ............................................................

俊偉 said...

謝謝你的分享,祝你生活永遠多彩多姿!.................................................................

育淑 said...

感謝您費心的分享您的生活!讓我也感同身受!

vickiekurt said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。......................................................................

致念致念 said...

人不能像動物一樣活著,而應該追求知識和美德.................................................................

柏辰柏辰 said...

人生是故事的創造與遺忘。............................................................

陳登陽 said...

當一個人內心能容納兩樣相互衝突的東西,這個人便開始變得有價值了。............................................................

香昱信張君林 said...

欣賞你的內容是美德~~支持你是最大回應..................................................................

舜佳 said...

Quietude is the crown of life.............................................................

吳婷婷 said...

No one knows the weight of anothers burden. ............................................................

魏江伶魏江伶 said...

知識可以傳授,智慧卻不行。每個人必須成為他自己。............................................................

方偉白方偉白 said...

Never hesitate to hold out your hand; never hesitate to accept the outstretched hand of another..................................................................

祥明祥明 said...

困難的不在於新概念,而在於逃避舊有的概念。............................................................

廖淑鄭志嘉娟 said...

良言一句三冬暖,惡語傷人六月寒。......................................................................

許李秀樺靜怡 said...

Make yourself necessary to someone..................................................................

lowe施cole鈺man駿 said...

一棵樹除非在春天開了花,否則難望在秋天結果。............................................................

洪志源 said...

這不過是滑一跤,並不是死掉而爬不起來了。.......................................................

承王蓁 said...

絕不要羞於承認自己不知道的事。..................................................

王雅吳玉婷奇 said...

蛛絲馬跡皆學問、落花水面皆文章............................................................

群育航學 said...

Everyone fastens where there is gain.............................................................

文王廷 said...

愛情是盲目的,但婚姻恢復了它的視力。..................................................................

峻胡邦慧v帆 said...

君子立恆志,小人恆立志。................. ................................................

小草 said...

我來湊熱鬧的~~^^ 要平安快樂哦..................................................

偉冠儒冠儒倫 said...

多謝美味的心靈雞湯......................................................

Jamila M.K said...

The post is very helpful to me.Thanks a lot.

mary Brown said...

Great Article
.net training online | C# Online Training | Dot Net Training in Chennai | .Net Training in Chennai | ASP.NET Training | ASP.NET MVC Training | Dot Net Interview Questions | Dot Net Training Institutes in Chennai