|
|
|
|
|
Home
»
OOP/OOD
»
Introduction to object oriented programming
|
|
|
|
Technologies:
.NET 1.0/1.1,Visual C# .NET
|
|
Total downloads :
|
|
|
Total page views :
|
5919
|
|
Rating :
|
|
3.75/5
|
|
This article has been rated :
|
4 times
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
A Programmer's Introduction to C# 2.0, Third Edition is a critical update to the highly successful second edition. It is written by a member of the original C# language-design team and a C# program manager, so you can be certain this book contains the expertise you're looking for.
|
|
|
|
|
|
|
|
|
|
|
|
|
Introduction
Object oriented programming is a style of programming where the code and data are bound together in objects, whose structures are base on class hierarchies.
You can also define object oriented programming as a collection of cooperating objects, each object is capable of receiving messages, processing data, and sending messages to other objects.
Each object can be viewed as an independent little machine with a distinct role.
Let's start the story from the beginning:
In any program you work with data, the program receive the data as input from the user, processing the data, and return the data as output to the user.
This data can be of any type like (numbers, text, boolean expression, etc) so when we dealing with data we store it in variables, any variable Should be of a certain data type that represents the type of data it can store.
The data type also defines the characteristics and behavior of the variable.
The .NET Framework has many data types you can use like:
int: for integer numbers string: for words and text bool: true or false char: for characters and so on
So if you want to store any data in a variable the variable must be of data type that suitable for this data. For example if you want to store an integer number in a variable we first create a variable of int.
int myNumber;
As you saw, to create our variable we start with the keyword that represents the data type (int) and then the name we gave to our variable. Then we can store our number in the variable.
myNumber = 55;
and the same thing with other data types.
int myAge = 23; string myName = "Amr Ashush"; char initials = 'M'; bool isAuthor = true;
User Defined Data types and Objects:
What if you want a data type that represents something in real world like (Car, Person, Employee, or any thing). As you may know these types doesn't exist in .NET.
If you want to deal with a thing (object) like a car in your program you need to define its data type (class) that defines its characteristics and behavior.
The Class Type:
A class is a custom user defined data type that defines the characteristics of a thing (object's fields or properties) and the behavior of the thing (methods).
Object-oriented languages allow the programmers to define data types that represent real world entities by grouping data and functionality in a single User Defined Type (UDT).
Let's see an example of creating Employee class (UDT):
//Our UDT Employee
public class Employee
{
//the class characteristics
public string firstName;
public string lastName;
public int Age;
public int ID;
//the class behavior
public void DoWork()
{
Consol.WriteLine("Employee {0} {1} doing his work", firstName, lastName);
}
//the default constructor
public Employee() { } }
As you saw we created a Employee class which define the Employee characteristics (an employee has first name, last name, age, and ID) and define also Employee behavior or what Employee can do (DoWork() method)
We now defined our data type let's see how we can use it to create Objects.
Example:
//creating the first object
Employee firstEmployee = new Employee();
//adding data to the first object
firstEmployee.firstName = "Amr";
firstEmployee.lastName = "Ashush";
firstEmployee.Age = 23;
firstEmployee.ID = 12345;
//creating the second object
Employee secondEmployee = new Employee();
//adding data to the second object
secondEmployee.firstName = "Jack";
secondEmployee.lastName = "Bauer";
secondEmployee. Age = 40;
secondEmployee.ID = 125;
//Ask the first Employee to do work firstEmployee.DoWork();
The result:
Amr Ashush doing his work.
//Ask the second Employee to do work secondEmployee.DoWork();
The result:
Jack Bauer doing his work.
As you can see we created tow objects (Employees) each one has its own data and behavior. So as I said previously each object can viewed as independent little machine with a distinct role and responsibility. So when we assign any data to an object it assigned only to this object unless there is some objects points to the same value in the heap(will talk about this later).
The main pillars of Object-Oriented Programming:
Any object-oriented language has three main pillars:
- Encapsulation: The ability of hiding object's internal implementation.
- Inheritance: The ability of building a new class definition based on existing class definition.
- Polymorphism: The ability of treating related objects the same way.
First pillar of OOP -Encapsulation:
Encapsulation gives you the ability to hide unnecessary implementation details from the object user. It simplifies the programming tasks instead of writing many lines of code.
As you saw in the last example all we need to is create an object and call the do work method instead of writing all lines of code in the DoWork() method each time we want the object do the same functionality.
Another benefit of encapsulation is data protection so we must define object's data as private so the object user must ask in order to change or obtain the value so we can make all validation processes we need.
For more information about Encapsulation see my article:
The first pillar of object-oriented programming -Encapsulation
Second pillar of OOP -Inheritance:
Inheritance gives you the ability of building a new class definition based on existing class definition. Inheritance allows you to extend the behavior of a base class by enabling a sub class to inherit its core functionality.
For example our we can extend our class Employee by allow a sub class named SalesEmployee to inherit it
//This class has the same functionality of the Employee class public class SalesEmployee : Employee { }
Third pillar of OOP -Polymorphism:
Polymorphism gives you the ability of treating related objects the same way. Polymorphism allow the base class to define a set of members to all descendents, when derived types override the members defined by the base class, they are redefining how they respond to the same request.
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Amr Monjid
Amr Monjid is a computer programmer from Egypt. He has experience with C#, .Net framework, ASP.NET, Windows Applications, ADO.NET, Xml, Web Services, Custom Controls, and e-commerce web sites. and he is a MCTS: .NET framework 2.0, Windows Application, Distributed Aplication.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other
Visual Studio release. Work more productively and collaboratively-with
greater control over your work at every step. The Beta 2 can give you a
head start on achieving efficiency.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|