OBJECT ORIENTED
PROGRAMMING IN C#
Course Structure
 Class
 Encapsulation
 Inheritance
 Polymorphism
 Abstract Class
 Interface
 Type casting
C# Class Type
class NameOfClass
{
}
//Example:
class Car
{
public int currSpeed;
}
Class and Object
Car redCar = new Car(“red”);
Car yellowCar = new
Car(“yellow”);
Class Members
 Field
 Property
 Method
 Event
Constructors
 Initialize state of an object.
 Method with no return value (not even
void).
 Name of constructor is similar with class
name.
class Machine {
//no constructor means default constructor
}
class Gear {
//custom default constructor
public Gear() {}
}
Trivia #1
class A {
public A() { Console.WriteLine("A"); } }
class B : A {
public B() { Console.WriteLine("B"); } }
class C : B {
public C() { Console.WriteLine("C"); } }
class Program {
static void Main() {
C c = new C();
}
}
Static Keyword
 Class level member.
 Can be applied to:
 Class (static class)
 Method (static method)
 Field (static field)
 Constructor (static constructor)
Trivia #2
class D {
public static int I;
}
class Program {
static void Main() {
D d = new D();
d.I = 9;
}
}
Encapsulation
On
Off
class Machine {
private Gear[] gears;
private int temperature;
public int Temperature {
get { return temperature;}
set { temperature = value;}
}
public void TurnOn() {}
public void TurnOff() {}
}
Encapsulation
 Can be done by making fields private.
 Use setter and getter methods.
 Use property for better flexibility.
 Expose as few as public class members.
Access Modifier
Access Modifier Applied to Description
public Types or type
members
No access restriction. Can be accessed from
any objects, any derived classes and external
assemblies
private Type
members or
nested types
Can only be accessed by the class (or structure)
the defines the item.
protected Type
members or
nested types
Not accessible from other objects, but can be
accessed from derived classes.
internal Type or type
members
Can only be accessed within the current
assembly.
protected
internal
Type
members or
nested types
Can be accessed within the current assembly,
the defining class and derived classes.
Trivia #3
class D {
int I;
}
class Program {
static void Main() {
D d = new D();
d.I = 10;
}
}
Object Composition
Wheel
Car
HAS-A
class Car {
private Wheel wheel1, wheel2, wheel3,
wheel4;
}
4
Inheritance
Base Class
Derived
Class
Vehicle
Car
IS-A IS-A
Car is a
Vehicle
class Car : Vehicle {
}
Trivia #4
class A {}
class D : A { }
class B : A {
D d;
};
class C {
B b;
};
A. A is D
B. B is D
C. B has D
D. B is A
E. B has C
Inheritance Example
Base Class Derived Class
Student GraduateStudent, UndergraduateStudent
Shape Circle, Triangle, Rectangle, Sphere, Cube
Loan CarLoan, HomeImprovementLoan, MortgageLoan
Employee Faculty, Staff
Account CheckingAccount, SavingsAccount
Inheritance Example
Vehicle
Truck Motorcycle
Car
Vehicle
+ Move()
Overloading & Overriding
 Overload: multiple methods with same
name but different parameter list.
 Override: multipe methods in the different
hierarchy with same name and same
parameter list.
Polymorphism
 Polymorphism is the ability of object type
acts as their base class type.
 Polymorphism means “many forms”.
 Real object type is determined at runtime.
Polymorphism Example
Vehicle
Truck Motorcycle
Car
Vehicle
+ Move()
Abstract Class
 Class that can not be instantiated.
 Abstract class contains at least one
abstract function.
abstract class Shape {
public abstract void Draw();
}
Abstract Class Example
Shape
Rectangle Triangle
Circle
Shape
+ Draw()
+
CalculateArea()
Interface
 a class that specifies a set of functions that
are to be implemented by other classes to
provide a standardized way of providing
some specific functionality.
Interface Example
public interface ICanvasDrawable {
void DrawOnCanvas(Canvas c);
}
public class Circle : Shape, ICanvasDrawable {
public void Draw() { Console.WriteLine(“draw circle”);}
public void DrawOnCanvas(Canvas c) {
c.DrawShape(this);
}
}
Type Casting
//Employee is-a Person
Person p1 = new Employee();
Employee e1 = (Employee) p1;
//as keyword
Employee e2 = p1 as Employee;
//is keyword
if (p1 is Employee) {}
System.Object
Instance Method
of Object Class
Meaning
Equals() By default, this method returns true only if the items being
compared refer to the exact same item in memory.
GetHashCode() This method returns an int that identifies a specific object
instance.
GetType() This method returns a Type object that fully describes the
object you are currently referencing.
ToString() This method returns a string representation of this object,
using
the <namespace>.<type name> format (termed the fully
qualified name).
Finalize() This method is called to free any allocated resources before
the
object is destroyed.
References
 Troelsen, Andrew. 2007. Pro C# 2008 and
the .NET 3.5 Platform, 4th edition.
 Cwalina, Krzystof; Abrams, Brad. 2009.
Framework Design Guidelines, 2nd edition.
Next.......
Exception Handling and
Object Lifetime

basic objec oriented programming C# fundamental.pptx

  • 1.
  • 2.
    Course Structure  Class Encapsulation  Inheritance  Polymorphism  Abstract Class  Interface  Type casting
  • 3.
    C# Class Type classNameOfClass { } //Example: class Car { public int currSpeed; }
  • 4.
    Class and Object CarredCar = new Car(“red”); Car yellowCar = new Car(“yellow”);
  • 5.
    Class Members  Field Property  Method  Event
  • 6.
    Constructors  Initialize stateof an object.  Method with no return value (not even void).  Name of constructor is similar with class name. class Machine { //no constructor means default constructor } class Gear { //custom default constructor public Gear() {} }
  • 7.
    Trivia #1 class A{ public A() { Console.WriteLine("A"); } } class B : A { public B() { Console.WriteLine("B"); } } class C : B { public C() { Console.WriteLine("C"); } } class Program { static void Main() { C c = new C(); } }
  • 8.
    Static Keyword  Classlevel member.  Can be applied to:  Class (static class)  Method (static method)  Field (static field)  Constructor (static constructor)
  • 9.
    Trivia #2 class D{ public static int I; } class Program { static void Main() { D d = new D(); d.I = 9; } }
  • 10.
    Encapsulation On Off class Machine { privateGear[] gears; private int temperature; public int Temperature { get { return temperature;} set { temperature = value;} } public void TurnOn() {} public void TurnOff() {} }
  • 11.
    Encapsulation  Can bedone by making fields private.  Use setter and getter methods.  Use property for better flexibility.  Expose as few as public class members.
  • 12.
    Access Modifier Access ModifierApplied to Description public Types or type members No access restriction. Can be accessed from any objects, any derived classes and external assemblies private Type members or nested types Can only be accessed by the class (or structure) the defines the item. protected Type members or nested types Not accessible from other objects, but can be accessed from derived classes. internal Type or type members Can only be accessed within the current assembly. protected internal Type members or nested types Can be accessed within the current assembly, the defining class and derived classes.
  • 13.
    Trivia #3 class D{ int I; } class Program { static void Main() { D d = new D(); d.I = 10; } }
  • 14.
    Object Composition Wheel Car HAS-A class Car{ private Wheel wheel1, wheel2, wheel3, wheel4; } 4
  • 15.
  • 16.
    Trivia #4 class A{} class D : A { } class B : A { D d; }; class C { B b; }; A. A is D B. B is D C. B has D D. B is A E. B has C
  • 17.
    Inheritance Example Base ClassDerived Class Student GraduateStudent, UndergraduateStudent Shape Circle, Triangle, Rectangle, Sphere, Cube Loan CarLoan, HomeImprovementLoan, MortgageLoan Employee Faculty, Staff Account CheckingAccount, SavingsAccount
  • 18.
  • 19.
    Overloading & Overriding Overload: multiple methods with same name but different parameter list.  Override: multipe methods in the different hierarchy with same name and same parameter list.
  • 20.
    Polymorphism  Polymorphism isthe ability of object type acts as their base class type.  Polymorphism means “many forms”.  Real object type is determined at runtime.
  • 21.
  • 22.
    Abstract Class  Classthat can not be instantiated.  Abstract class contains at least one abstract function. abstract class Shape { public abstract void Draw(); }
  • 23.
    Abstract Class Example Shape RectangleTriangle Circle Shape + Draw() + CalculateArea()
  • 24.
    Interface  a classthat specifies a set of functions that are to be implemented by other classes to provide a standardized way of providing some specific functionality.
  • 25.
    Interface Example public interfaceICanvasDrawable { void DrawOnCanvas(Canvas c); } public class Circle : Shape, ICanvasDrawable { public void Draw() { Console.WriteLine(“draw circle”);} public void DrawOnCanvas(Canvas c) { c.DrawShape(this); } }
  • 26.
    Type Casting //Employee is-aPerson Person p1 = new Employee(); Employee e1 = (Employee) p1; //as keyword Employee e2 = p1 as Employee; //is keyword if (p1 is Employee) {}
  • 27.
    System.Object Instance Method of ObjectClass Meaning Equals() By default, this method returns true only if the items being compared refer to the exact same item in memory. GetHashCode() This method returns an int that identifies a specific object instance. GetType() This method returns a Type object that fully describes the object you are currently referencing. ToString() This method returns a string representation of this object, using the <namespace>.<type name> format (termed the fully qualified name). Finalize() This method is called to free any allocated resources before the object is destroyed.
  • 28.
    References  Troelsen, Andrew.2007. Pro C# 2008 and the .NET 3.5 Platform, 4th edition.  Cwalina, Krzystof; Abrams, Brad. 2009. Framework Design Guidelines, 2nd edition.
  • 29.