You are on page 1of 36

OOPS

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?

Relationships Among Objects


Hierarchical Relationship
Containment Relationship
What Is an Interface?
An interface is a device or a system that
unrelated entities use to interact
When to Use Interfaces
 Interfaces are better suited to situations
in which your applications require many
possibly unrelated object types to provide
certain functionality

 Interfaces are more flexible than base


classes because you can define a single
implementation that can implement
multiple interfaces.
When to Use Interfaces
 Interfaces are better in situations in which
you do not need to inherit implementation
from a base class.

 Interfaces are useful in cases where you


cannot use class inheritance.
E.g. structures cannot inherit from
classes, but they can implement
interfaces.
Structs
 Structures are suitable to represent
lightweight objects
 Syntactically like classes, but more
efficient
 Stored in-line, not heap allocated
 Assignment copies data, not reference
 Always inherit directly from
System.Object
 Ideal for light weight objects
 Complex, Point, Rectangle, Color
 int, float, double, etc., are all structs
 No heap allocation, less GC pressure
Classes and Structs
class CPoint { int x, y; ... }
struct SPoint { int x, y; ... }

CPoint cp = new CPoint(10, 20);


SPoint sp = new SPoint(10, 20);
10
sp
20

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

Stream Hashtable int double

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.

Access Levels for Methods


 you should declare class members with
access modifiers that provide the least
amount of access feasible.
 Internal class members should be
declared as Private
Access Levels for Methods  
 Methods that are only used within a class
or by descendants of a class should use
the Protected access modifier.
Access Levels for Methods
 Internal data members can be accessed
from outside a class but only by modules
that are part of the project where the
class is defined.
Properties
Properties promote encapsulation by
allowing a class or struct to control access
to its data and by hiding the internal
representation of the data.
public class Button: Control
{
private string text;

public string Text {


get {
return text;
}
set {
text = value; Button b = new Button();
Repaint(); b.Text = "OK";
} string s = b.Text;
}
}
Attributes
 How do you associate information
with types and members?
 Select a class as serializable
 Identify a method as deprecated
 Select methods as web services
 Traditional solutions
 Add keywords or pragmas to language
 Use external files (e.g., .IDL, .DEF)
 C# solution: Attributes
Attributes
 Completely extensible
 Inherit from System.Attribute, apply
AttributeUsage attribute
 Type-safe
 Arguments checked at compile-time
 Examined using reflection at run-time
 Extensive use in .NET frameworks
 XML, Web Services, security,
serialization, component model, COM
and P/Invoke interop, code
configuration…
[AttributeUsage(System.AttributeTargets.Property)]
public class Category: System.Attribute
{
public readonly string Value;
public Category(string s){Value = s;}
}

public class Button: Control


{
[Category("Appearance")]
[Description("Color of text in button")]
[Browsable(true)]
public Color TextColor {...}
}

Type type = typeof(Button);


PropertyInfo pi = type.GetProperty("TextColor");

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";}
}

public class Orange : Fruit {


public new string Colour() {return "Orange";}
public override string Shape() {return "Round";}
}

Fruit someFruit = new Orange();


string a = someFruit.Shape(); // "Round"
string b = someFruit.Colour(); // "You tell me"
Orange anOrange = (Orange)someFruit;
string c = anOrange.Shape(); // "Round"
string d = anOrange.Colour(); // "Orange"
Early binding & Late binding
 Early binding means that our code directly
interacts with the object, by directly
calling its methods. Since the compiler
knows the object's data type ahead of
time, it can directly compile code to
invoke the methods on the object.

 Late binding means that our code


interacts with an object dynamically at
run-time. This provides a great deal of
flexibility since our code literally doesn't
care what type of object it is interacting
with as long as the object supports the
methods we want to call.
Conclusion
Object-Oriented Programming (OOP) is a
software development paradigm that
suggests developers to split a program in
building blocks known as objects. OOP is
full of abstract concepts, and the best
approach to understand them is practical
and not only theoretical.

GO, Play with OOPS

You might also like