Você está na página 1de 8

1

Object oriented programming features helps the program ad there by users of the application to achieve
increased performance, it saves time of developing the application, give optimized code for the application,
helps in gaining secured applications and there by helps in easier maintenance.

Classes and Objects:


The basic building blocks of object-oriented programming are the class and the object. A class defines the
available characteristics and behavior of a set of similar objects and is defined by a programmer in code. A
class is an abstract definition that is made concrete at run-time when objects based upon the class are
instantiated and take on the class' behavior.

Objects:
This is the basic unit of object oriented programming. That is both data and function that operate on data are
bundled as a unit called as object.

Classes:
The concept of class is similar to the concept of structure in C. In other words classes are the data types on
which objects are created. So while a class is created no memory is allocated only when an object is created
memory gets allocated.

Inheritance:
As the name suggests Inheritance is the process of forming a new class from an existing class that is from
the existing class called as base class, new class is formed called as derived class. This is a very important
concept of object oriented programming since this feature helps to reduce the code size.

It is the process by which one object acquires the properties of another object. This supports the hierarchical
classification. Without the use of hierarchies, each object would need to define all its characteristics
explicitly. However, by use of inheritance, an object need only define those qualities that make it unique
within its class. It can inherit its general attributes from its parent. A new sub-class inherits all of the
attributes of all of its ancestors.

To create a derived class in C#, the class declaration should be done as:

class child: parent

To create a derived class in VB.NET, the class declaration should be done as:

Class child
Inherits parent
End Class

Multiple inheritance

Multiple inheritance is the possibility that a child class can have multiple parents. Human beings have
always two parents, so a child will have characteristics from both parents.

In OOP, multiple inheritance might become difficult to handle because it allows ambiguity for the compiler.
There are programming languages such as C++ that allow multiple inheritance; however, other programming
languages such as Java and the .NET Framework languages do not allow multiple inheritance. Multiple
inheritance can be emulated in .NET using Multiple Interface Inheritance.

Sealed class

A sealed class is a class that does not allow inheritance. Some object model designs need to allow the
creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.

To create a sealed class in C#, the class declaration should be done as:

sealed class Shape


2

To create a sealed class in VB.NET, the class declaration should be done as:

NonInheritable Class Shape

Data Abstraction:
By this feature of object oriented programming it is possible to represent the needed information in program
without presenting the details. Also by the feature of data abstraction it is possible to create user defined
data types and thus increase the power of programming language. Abstraction is the process of
representing simplified versions of real-world objects in your classes and objects.

An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes
contain one or more abstract methods that do not have implementation. Abstract classes allow specialization
of inherited classes.

To create an abstract class in C#, the class declaration should be done as:

abstract class Shape

To create an abstract class in VB.NET, the class declaration should be done as:

MustInherit Class Shape

To following code shows a sample implementation of an abstract class:

/// C#
using System;
namespace DotNetTreats.OOSE.OOPSamples
{
public abstract class Shape
{
private float _area;
private System.Drawing.Color _color;
private float _perimeter;
public float Area
{
get
{
return _area;
}
set
{
_area = value;
}
}
public System.Drawing.Color Color
{
get
{
return _color;
}
set
{
_color = value;
}
}
public float Perimeter
{
get
{
return _perimeter;
3

}
set
{
_perimeter = value;
}
}
public abstract void CalculateArea();
public abstract void CalculatePerimeter();
}
}

Data Encapsulation:
Data Encapsulation is the process of combining data and functions into a single unit called class. By this
method one cannot access the data directly. Data is accessible only through the functions present inside the
class. Thus Data Encapsulation gave rise to the important concept of data hiding.

It is the mechanism that binds together code and data in manipulates, and keeps both safe from outside
interference and misuse. In short it isolates a particular code and data from all other codes and data. A well-
defined interface controls the access to that particular code and data.

Polymorphism:
The ability to use an operator or function in different ways in other words giving different meaning or
functions to the operators or functions is called polymorphism. Poly refers many. That is a single function or
an operator functioning in many ways different upon the usage is called polymorphism.

It is a feature that allows one interface to be used for general class of actions. The specific action is
determined by the exact nature of the situation. In general polymorphism means "one interface, multiple
methods", This means that it is possible to design a generic interface to a group of related activities. This
helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is
the compiler's job to select the specific action (that is, method) as it applies to each situation.

To following sample code shows an implementation of a derived class (rectangle).


Polymorphism represented in the Rectangle's methods.

/// C#
using System;
namespace DotNetTreats.OOSE.OOPSamples
{
class Rectangle : Shape
{
private float _height;
private float _width;
public rectangle(float height, float width)
{
_height = height;
_width = width;
}
public float Height
{
get
{
return _height;
}
set
{
_height = value;
}
}
public float Width
{
get
{
4

return _width;
}
set
{
_width = value;
}
}
public override void CalculateArea()
{
this.Area = _height * _width;
}
public override void CalculatePerimeter()
{
this.Perimeter = (_height * 2) + (_width * 2);
}
}
}

Overloading
Overloading allows multiple functions to have the same signature but different parameters to perform
different functions. The concept of overloading is also a branch of polymorphism. When the exiting operator
or function is made to operate on new data type it is said to be overloaded.

Overriding
When the implementation of a method of a base class is modified or replaced in the derived class it is called
overloading. By default, a derived class inherits methods from its base class. If an inherited property or
method needs to behave differently in the derived class it can be overridden; that is, you can define a new
implementation of the method in the derived class.

Virtual keyword

The virtual keyword allows polymorphism too. A virtual property or method has an implementation in the
base class, and can be overriden in the derived classes.

To create a virtual member in C#, use the virtual keyword:

public virtual void Draw()

To create a virtual member in VB.NET, use the Overridable keyword:

Public Overridable Function Draw()

Override keyword

Overriding is the action of modifying or replacing the implementation of the parent class with a new one.
Parent classes with virtual or abstract members allow derived classes to override them.

To override a member in C#, use the override keyword:

public override void CalculateArea()

To override a member in VB.NET, use the Overrides keyword:

Public Overrides Function CalculateArea()

Reusability:
That is object oriented programming has the feature of allowing an existing class which is written and
debugged to be used by other programmers and there by provides a great time saving and also code
5

efficiency to the language. Also it is possible to a have the existing class and adds new features to the
existing class as pet the programmer’s choice.

Modularity
Object-oriented programming also permits increased modularity. Individual classes or groups of linked
classes can be thought of as a module of code that can be re-used in many software projects. This reduces
the need to redevelop similar functionality and therefore can lower development time and costs.

Class members
A class has different members, and developers in Microsoft suggest to program them in the following order:
• Namespace: The namespace is a keyword that defines a distinctive name or last name for the
class. A namespace categorizes and organizes the library (assembly) where the class belongs and
avoids collisions with classes that share the same name.
• Class declaration: Line of code where the class name and type are defined.
• Fields: Set of variables declared in a class block.
• Constants: Set of constants declared in a class block.
• Constructors: A method or group of methods that contains code to initialize the class.
• Properties: The set of descriptive data of an object.
• Events: Program responses that get fired after a user or application action.
• Methods: Set of functions of the class.
• Destructor: A method that is called when the class is destroyed. In managed code, the Garbage
Collector is in charge of destroying objects; however, in some cases developers need to take extra
actions when objects are being released, such as freeing handles or deallocating unmanaged
objects. In .NET, there is no concept of deterministic destructors. The Garbage Collector will call
the Finalize() method at a non-deterministic time while reclaiming memory for the application.

Access keywords
Access keywords define the access to class members from the same class and from other classes. The
most common access keywords are:
• 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.

Sample class implementation in C#

/// C#
///Imported namespaces
using System;
/// Namespace: Consider using CompanyName.Product.ComponentType
namespace DotNetTreats.OOSE.OOP_CSharp
{
///Class declaration
public class employee
{
///Fields
private string _name;
private int _salary;
///Constants
private const int anualBonus = 1000;
///Constructor
public employee()
{
6

}
///Properties
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public int Salary
{
get
{
return _salary;
}
set
{
_salary = value;
}
}
/// Event handlers
public event EventHandler OnPromotion
{
add
{
}
remove
{
}
}
/// Methods
public void DuplicateSalary()
{
_salary = _salary*2;
}
}
}

Sample class implementation in VB.NET

' VB.NET
'Imported namespaces
Imports System
' Namespace: Consider using CompanyName.Product.ComponentType
Namespace DotNetTreats.OOSE.OOP_VBNET
'Class declaration
Public Class employee
'Fields
Private _name As String
Private _salary As Integer
'Constants
Private Const anualBonus As Integer = 1000
'Constructors
Public Sub New()
MyBase.New()
End Sub
'Properties
Public Property Name() As String
7

Get
Return _name
End Get
Set(ByVal Value As String)
_name = value
End Set
End Property
Public Property Salary() As Integer
Get
Return _salary
End Get
Set(ByVal Value As Integer)
_salary = value
End Set
End Property
' Event handlers
Public Event OnPromotion As EventHandler
'Methods
Public Sub DuplicateSalary()
_salary = (_salary * 2)
End Sub
End Class
End Namespace

Object

Objects are the building blocks of OOP and are commonly defined as variables or data structures that
encapsulate behavior and data in a programmed unit. Objects are items that can be individually created,
manipulated, and represent real world things in an abstract way.

Object composition

Every object is composed by:

• Object identity: Means that every object is unique and can be differentiated from other objects.
Each time and object is created (instantiated) the object identity is defined.
• Object behavior: What the object can do. In OOP, methods work as functions that define the set of
actions that the object can do.
• Object state: The data stored within the object at any given moment. In OOP, fields, constants,
and properties define the state of an object.

Structures

Not everything in the real world should be represented as a class. Structures are suitable to represent
lightweight objects. Structures can have methods and properties and are useful for defining types that act as
user-defined primitives, but contain arbitrary composite fields. The .NET Framework defines some structures
such as System.Drawing.Rectangle, System.Drawing.Point, and System.Drawing.Color.

Sample structure implementation in C#

/// C#
struct Point
{
private int _x;
private int _y;
Point(int x, int y)
{
this._x = x;
8

this._y = y;
}
public int X
{
get
{
return _x;
}
set
{
_x = value;
}
}
public int Y
{
get
{
return _y;
}
set
{
_y = value;
}
}
}

Sample structure implementation in VB.NET

' VB.NET

Structure Point
Private _x As Integer
Private _y As Integer
Sub New(ByVal x As Integer, ByVal y As Integer)
MyBase.New()
Me._x = x
Me._y = y
End Sub
Public Property X() As Integer
Get
Return _x
End Get
Set(ByVal Value As Integer)
_x = value
End Set
End Property
Public Property Y() As Integer
Get
Return _y
End Get
Set(ByVal Value As Integer)
_y = value
End Set
End Property
End Structure

Você também pode gostar