Professional Documents
Culture Documents
Jomy John
What is OOPs?
A method of computer programming
where items of related data together with
routines associated with it are treated as
a single 'object' or item in the program.
Why OOPS?
Closer to real life representation
Hierarchical relationships can be
represented
Code Reusability
Simpler Debugging
Faster Development Time
Features of OOPS
Emphasis is on DATA rather than Proc.
Programs are divided into Objects.
Data is hidden and cannot be accessed by
external functions.
Objects may communicate with each
other through functions.
Benefits of OOP
Eliminate redundant code and extend the
use of existing classes through
inheritance.
Data hiding helps the programmer to build
secure programs.
Software complexity can be easily
managed
Easy to partition the work in a project
based on objects
Concepts of OOP
Objects
Classes
Data abstraction & encapsulation
Inheritance
Polymorphism
Dynamic Binding
What Is a Class?
A class is a blueprint or prototype that
defines the data and the behavior
common to all objects of a certain kind.
What is an Object?
An instance of a class.
The act of creating an object is called
instantiation.
Object Lifetime:
How Objects Are Created and Destroyed?
cp CPoint
10
20
Value and Reference Types
Value types
Variables directly contain data
Cannot be null
Reference types
Variables contain references to objects
May be null
int i = 123;
i 123 string s = "Hello world";
s "Hello world"
Value and Reference Types
Value types
Primitives int i; double x;
Enums enum State { Off, On }
Structs struct Point { int x, y; }
Reference types
Classes class Foo: Bar, IFoo {...}
Interfaces interface IFoo: IBar {...}
Arrays Foo[] a = new Foo[10];
Delegates delegate void Empty();
Unified Type System
All types ultimately inherit from object
An implicit conversion exists from any
type to type object
object
MemoryStream FileStream
Unified Type System
Boxing
Allocates box, copies value into it
Unboxing
Checks type of box, copies value out
Shallow Copy and Deep Copy
int i = 123;
object o = i;
i
123 int j = (int)o;
o
System.Int32
j 123
123
Methods
Set of functions of the class.
foreach (Attribute a in
pi.GetCustomAttributes(false))
{
Category ca = a as Category;
if (ca != null) Console.WriteLine(ca.Value);
}
Delegates & Events
A delegate is a type that defines a method
signature
An event is defined in C# as 'a member
that enables an object or class to provide
notifications'.
Encapsulation
Encapsulation is the ability to contain and
control access to a group of associated
items.
Inheritance
Classes that serve as a basis for new
classes are called base classes. Classes
derived from base classes are called
derived classes. Derived classes inherit all
the fields, properties, methods, and
events of the base class.
When to Use Inheritance
Your inheritance hierarchy represents an
"is-a" relationship and not a "has-a"
relationship.
When to Use Inheritance
You can reuse code from the base classes.
You need to apply the same class and
methods to different data types.
The class hierarchy is reasonably shallow,
and other developers are not likely to add
many more levels.
You want to make global changes to
derived classes by changing a base class.
Classes
Inheritance
Single base class
Multiple interface implementations
Class members
Abstract, static and const
Nested types
Member access
Public, private, protected, internal
Polymorphism
Polymorphism refers to the ability to
define multiple classes with functionally
different, yet identically named methods
or properties that can be used
interchangeably by client code at run
time.
Inheritance-Based Polymorphism
Inheritance-based polymorphism involves
defining methods in a base class and
overriding them with new implementations
in derived classes.
Interface-Based Polymorphism
To achieve polymorphism with interfaces,
you implement an interface in different
ways in several classes.
Advantage: you do not need to re-compile
existing client applications to get them to
work with new interface implementations.
Polymorphism in C#
public class Fruit {
public virtual string Shape() {return "Not sure";}
public string Colour() {return "You tell me";}
}