Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » OOP/OOD » Introduction to object oriented programming

Introduction to object oriented programming

This article will provide you an overview about the object-oriented programming main pillars (Encapsulation, Inheritance, and Polymorphism).

Page Views : 16366
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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:

  1. Encapsulation: The ability of hiding object's internal implementation.
  2. Inheritance: The ability of building a new class definition based on existing class definition.
  3. 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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by Rahul Chowdhury on Feb 20, 2011
good one
 [Top] Rate this article
 
 About the author
 
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.
Looking for C# Consulting?
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.