Object Oriented Programming: Overview
Object Oriented Programming: Overview
Overview :
Object Oriented Approach
Access Specifiers
Access Modifiers
Inheritance
Properties
Polymorphism
Overloading
Function Overloading
Overriding
Interface
Abstract
Sealed
Collection
Indexers
Delegates
Events
Object Oriented Approach
The concept behind the object-oriented approach is to combine data and its
An object’s functions provide the only way to access its data. Thus, the data
Object maps the software model to real word concept. These objects have
Class
A group of objects which have similar data and methods is called a class.
Object
For example if student is a class, then Michael and John are instances. The data
components that are common among these objects are Name, Batch code etc., and the
Inheritance
Inheritance is the process of creating a new class, called the derived class, from an
Reusability
The concept of inheritance provides for reusability of existing classes and their
functions.
Polymorphism
Every object has a life cycle, from using class definition to its destruction.
Constructor
Destructor
Constructor
an instance of a class.
The constructor method should have the same name as the class.
Destructor
A destructor is a method with the same name as the class preceded by the
symbol, ~.
A destructor cannot take parameters. & A class can have only one destructor.
Garbage collection:
Is a process that automatically frees the memory of objects that are no more in
use.
Identifies the objects that are no more referenced in the program and releases
Access specifiers are used to limit the accessibility of the data members and functions
in a class. The accessibility of class members can be specified using the keywords:
Public
Protected
Private
Internal
Protected internal
Access Specifier
private - member accessible only from code that is part of the class
internal - member accessible only from code within the project (assembly)
where it is defined.
protected - member accessible only from code that is part of the class or a
derived class.
protected internal - member accessible from code derived classes within the
project or assembly.
Access Modifiers
Modifiers determine how the data members and methods are used in other
The different access modifiers that can be used in a C-Sharp program are:
static
sealed
abstract
virtual
Access Modifier - Static
A static modifier allows a variable or method to be associated with its class and
An abstract class can act as the base class for other classes.
It should be subclassed.
Inheritance
This is done by creating classes that derive properties from other classes and
The class from which the derived class derives properties is known as the base
class.
The Object class is the ultimate base class for all classes.
A derived class extends its direct base class. A derived class can add new
inherited member.
A derived class can hide inherited members by declaring new members with
the same name or signature. Note however that hiding an inherited member
does not remove the member - it merely makes the member inaccessible in the
derived class.
Properties
A property is a member that holds data of an object or a class. Properties are the
natural extensions of fields both are named members with associated types and the
Properties are classified as Static & Instance. Static property is not associated with a
The accessor of a property contains the executable statements associated with getting
property type. And the body of a get accessor must conform to the rules for value
returning methods.
Set accessor corresponds to a method with a single value parameter of the property
For example , an operation may exhibit different behavior in different instances. The
static binding
dynamic binding
Binding
It refers to the linking of a method call to the code to be executed in response to the call.
Static Binding
The object checks for a method call in the class. The method when found is
executed. when this happens at the compile time, it is called static polymorphism.
Dynamic Binding
Sometimes it is not possible for the compiler to decide which method should be
called. It means that the code associated with a given procedure call is not known
until the time of the call at runtime. this happens when the same method name is
Overloading takes place within the same class i.e., Overloaded forms of same
Where as in overriding when the class is subclassed and the function is made
Function overloading is the concept of using the same name for two or more
functions.
Method name
Type of parameters
Sequence of parameters
Number of parameters
The resolution of an overloaded function depends on the list of arguments and a set
follows:
candidate functions.
A function is a best member function, when its arguments match the function called.
In object creation
In array creation
Overriding
The keyword override is used to define a method in the derived class that
from an interface.
Methods
Properties
Indexers
Events
Abstract Class
A class that provides common behavior across a set of subclasses, but is not
itself designed to have instances that work.
If you would like to make classes that only represent base classes, and
don’t want anyone to create objects of these class types.
members.
Interface cannot provide any implementation for the methods and properties
A class can inherit only from a single class and its uses inherits keyword for
doing so. A class can implement multiple interfaces and uses implements
abstract class.
this Keyword
As with base, this can also be used within class members, and like base, this
Nested class
Collections are the objects that can contain arrays of other objects and contain
functionality that controls access to the objects. Collection classes are generally used
for maintains list of objects and may expose additional functionality over arrays.
namespace.
IList – Provides list of items for collection as well as capability for accessing these
elements.
Types of Collections
Stack -> L I F O
Queue -> F I F O
ArrayList -> Array is whose size can increase and decrease dynamically. It
can hold items of different types. It can increase and decrease size dynamically
you don't have to use REDIM keyword . You can access any item in array
Hashtable -> You can access array using INDEX value of array, but how many
times you know the real value of index. Hashtable provides way of accessing
the index using a user identified KEY value, thus removing the INDEX
problem.
It is a special kind of property that we can add to a class to provide array like
access. However we can define and use complex parameter type with the square
brackets syntax.
Delegates
object. The advantage is object Oriented, type safe and secure. Multicast delegate is
Events
Events are similar to exceptions in that they are raised by objects and we can supply
code that acts on them. It is a member that enables an object or class to provide
notifications. When an event occurs, all the event handlers are 'notified'. The event
declaration should have the keyword 'event' followed by the delagate type.
Exceptions
IndexOutOfRangeException
NullReferenceException
ArgumentException
ArrayTypeMismatchException
OverflowException
ArithmeticException
DivideByZeroException
InvalidOperationException
Structure are value types and classes are reference types. So structures use
stack and classes use heap.
Generics
Partial Types
Nullable Types
Iterator
Anonymous Methods
Static Class
Generics
A generic method might use its type parameter as the type of its return
value, or as the type of one of its formal parameters.
public T Field;
When you create an instance of a generic class, you specify the actual
types to substitute for the type parameters.
Each source file contains a section of the class definition, and all parts are
combined when the application is compiled.
To split a class definition, use the partial keyword modifier, as shown below:
}
public partial class Employee
Nullable Type
A nullable type can represent the normal range of values for its underlying
value type, plus an additional null value.
For example,
A Nullable<bool> can be assigned the values true or false, or null. The ability to
assign null to numeric and Boolean types is particularly useful when dealing with
databases and other data types containing elements that may not be assigned a
value. For example, a Boolean field in a database can store the values true or
false, or it may be undefined.
Nullable types have the following characteristics:
Nullable types represent value-type variables that can be assigned the value of
null. You cannot create a nullable type based on a reference type. (Reference
types already support the null value.)
Assign a value to a nullable type in the same way as for an ordinary value
type, for example int? x = 10; or double? D = 4.108;
Use the HasValue and Value read-only properties to test for null and retrieve
the value, for example if(x.HasValue) j = x.Value;
The HasValue property returns true if the variable contains a value, or false
if it is null. The Value property returns a value if one is assigned, otherwise a
System.InvalidOperationException is thrown.The default value for a nullable
type variable sets HasValue to false. The Value is undefined.
Use the ?? operator to assign a default value that will be applied when a
nullable type whose current value is null is assigned to a non-nullable type,
for example int? x = null; int y = x ?? -1;
Nested nullable types are not allowed. The following line will not compile:
Nullable<Nullable<int>> n;
Iterator
The iterator code uses the yield return statement to return each element in
turn. yield break ends the iteration. For more information, see yield.
foreach(int x in SampleClass.Iterator2){ }
Static Classes
Static classes and class members are used to create data and functions that
can be accessed without creating an instance of the class.
Static class members can be used to separate data and behavior that is
independent of any object identity: the data and functions do not change
regardless of what happens to the object.
Static classes can be used when there is no data or behavior in the class
that depends on object identity.