You are on page 1of 23

Object-oriented programming using C#

Kitaw A.
Classes
In object-oriented programming, a class is an extensible template
for creating objects
A class is a blueprint that defines the variables and the methods
common to all objects of a certain kind
Every object is built from a class
An object is an instance of a class
Classes
Let's take an example of developing a movie rental system
You will need various information about the movies like title,
length, release date, category (i.e action, comedy, etc.)
List out the common behaviors of these movies: we rent movies,
register movies, return movies
Object:
title: Troy
MOVIE length: 163
releaseDate: May 13, 2004
title category: historical war
length
releaseDate
catagory
RegisterMovie()
Object:
RentMovie() title: Robin Hood
ReturnMovie() length: 140
releaseDate: May 14, 2010
category: historical drama
Classes
The simplest possible class declaration is as follows:
class YourClassName
{

}
Classes can be abstract (cannot be instantiated, only inherited) or
sealed (cannot be inherited)
A class contains fields, methods and properties
Classes
Fields
A field is a variable that is a member of a class
For example:
class Movie
{
string title, releaseData, category;
int length;
}
Classes
Fields
Fields allow the following modifiers:
static: static modifier
public internal private protected: access modifiers
readonly: read-only modifier
Classes: instance vs static members
Instance members are specific to object instances
Static members are shared between instances of a class, so they
can be thought of as global data for objects of a given class
Static properties and fields allow us access to data that is
independent of any object instances
Static methods allow us to execute commands related to the class
type but not specific to object instances
Classes
Methods
A method performs an action in a series of statements
A method can receive input data from the caller by specifying
parameters and output data back to the caller by specifying a
return type
A method can specify a void return type, indicating that it doesn’t
return any value to its caller
A method can also output data back to the caller via ref/out
parameters
Classes
Methods (Example)
class Movie
{
string title, releaseData, category;
int length;
void RegisterMovie(){
//logic
}
void RentMovie(){
//logic
}
void ReturnMovie(){
//logic
}
}
Classes
Methods
Methods allow the following modifiers:
virtual: method may be overridden
abstract: method must be overridden (only permitted in abstract
classes)
override: method overrides a base class method (must be used if
a method is being overridden)
public internal private protected: access modifiers
Classes
Properties
Similar to fields but more involved in that they can perform
additional processing before modifying state - and might not
modify state at all
They achieve this by possessing two function-like blocks, one for
getting the value of the property and one for setting the value of
the property
These blocks, also known as accessors, defined using get and set
keywords respectively, may be used to control the access level of
the property
Classes
Properties
class Movie
{
private int length=10;
public int Length{
get{return length;}
set{
length=value;
}
}
}
Classes
Properties
The get function returns value to the user of the property
The set function assigns a value to the field.
Here we can use the keyword value to refer to the value received
from the user of the property
Inheritance
Inheritance is one of the most important features of OOP
Any class may inherit from another, which means that it will have
all the members that the class it inherits from has
In OOP terminology, the class being inherited from is the parent
class (also known as the base class)
The one that inherits is the child class (also known as derived
class)
Note that classes in C# may only inherit from a single base class
Inheritance
To do this we simply put a colon after the class name, followed by
the base class name
public class MyDerived : MyBase
{
// class members
}
Inheritance
SeriesMovie is a Movie
public class SeriesMovie : Movie
{
byte seasonNumber,episodeNumber
}
Polymorphism
Polymorphism means having many forms
In object-oriented programming paradigm, polymorphism is often
expressed as 'one interface, multiple functions’
Can be implemented by:
Method overloading (static polymorphism)
Method overriding (dynamic polymorphism)
Polymorphism
Method overloading
You can have multiple definitions for the same function name in the same scope
The definition of the function must differ from each other by the types and/or the
number of arguments in the argument list
You cannot overload function declarations that differ only by return type
class MyClass{
public void MyMethod(){
Console.WriteLine(“Method is empty”);
}
public void MyMethod(int i){
Console.WriteLine(“Method contains ”+i);
}
}
Polymorphism
Method overriding
You can override methods defined by base classes
The methods must be defined as virtual within the base class
Child class must use override while overriding
class Base{
public virtual void MyMethod(){
Console.WriteLine(“Method in base class”);
}
}
class Child:Base{
public override void MyMethod(){
Console.WriteLine(“Method in child class”);
}
}
Exception handling
An exception is an error generated either in our code or in a function called by our
code that occurs at runtime
Example:
int[] myArray = {1, 2, 3, 4};
int myElem = myArray[4];
This generates the following exception: 'System.IndexOutOfRangeException’
And terminates the program
So, what exactly do we have to do to "handle" an exception?
Exceptions
try...catch...finally
try
{
...
}
catch (<exceptionType> e)
{
...
}
finally
{
...
}
Exceptions
try - contains code that might throw exceptions
catch - contains code to execute when exceptions are thrown
Catch blocks may be set to respond only to specific exception
types
It is also possible to set a general catch block that will respond to
all exceptions
finally - contains code that is always executed
either after the try block if no exception occurs
after a catch block if an exception is handled, or
just before an unhandled exception terminates the application
Exceptions
int[] myArray = { 1, 2, 3, 4 };
Console.WriteLine("Enter index of an element: ");
try
{
int index = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(myArray[index]);
}
catch(FormatException e)
{
Console.Write("Number format not correct: "+e);
}
catch(IndexOutOfRangeException e)
{
Console.Write("Invalid index: "+e);
}

You might also like