Ch3 Object-Oriented Programming in C#..
Ch3 Object-Oriented Programming in C#..
3 year Info
rd
Object-Oriented Programming in C#
1
Plan
Introduction
Methods
Classes/ Objects
Constructors and destructors
Properties
Inheriting and extending classes
Method overriding and polymorphism
Access modifiers
Encapsulation principles
Abstract classes and interfaces
2
Introduction
OOP stands for Object-Oriented Programming.
3
Methods (1/6)
A method is a block of code which only runs when it is called.
To create a method with no return value: static void
Methodname().
Example:
class Program
{
static void MyMethod()
{
// code to be executed
}
}
4
Methods (2/6)
To call a method: Methodname().
Example:
static void MyMethod()
{
Console.WriteLine(“Hello World!");
}
static void Main(string[] args)
{
MyMethod();
}
// Outputs “Hello World!"
5
Methods (3/6)
To pass parameters: Methodname(type parameter name).
Example:
static void Username(string fstname)
{
Console.WriteLine(fstname + " Refen");
}
14
Constructors and destructors (2/6)
Example:
class Hello
{
//Constructors
public Hello()
{
}
//Parameterized Constructor
public Hello(int num)
{
}
public Hello(string msg)
{
} 15
}
Constructors and destructors (3/6)
Destructors are used to removing all the instance of classes and releasing
resources. It works like a garbage collector.
while creating Destructors:
Destructor is used followed by tilde (~) Symbol.
A class can only have one destructor.
It cannot be called. It is invoked automatically.
It does not have parameters.
Example:
class Hello
{
~Hello() // destructor
{
// cleanup statements...
} 16
}
Constructors and destructors (4/6)
Example: create a class that requires a temperature value in centigrade and in then
use that value in entire class:
//Constructor Creation
public CalculateHeat(int val)
{
temp = Convert.ToInt32(val);
}
17
Constructors and destructors (5/6)
// Method creation that converts centigrade to Fahrenheit
public void calculate()
{
decimal fahrenheit = Convert.ToDecimal(temp * 1.8 + 32);
Console.WriteLine("{0} degree centigrade into fahrenheit = {1}", temp,
fahrenheit);
}
Output:
36 degree centigrade into fahrenheit = 96.8
I am second constructor
Destructor Initializes, Cleanup Process Complete
19
Properties (1/3)
A property is like a combination of a variable and a method, and it has two
methods: a get and a set method:
The get method returns the value of the variable.
The set method assigns a value to the variable.
Example:
class Person
{
private string name; // field
21
Properties (3/3)
C# also provides a way to use automatic properties, only write get; and set; inside the property.
Example:
class Person
{
public string Name // property
{
{ get; set; }
}
class Program
{
static void Main(string[] args)
{
Person myObj = new Person();
myObj.Name = "Liam";
Console.WriteLine(myObj.Name); //Output Liam
} 22
}
Inheriting and extending classes (1/4)
In C#, it is possible to inherit fields and methods from one class to another. The "inheritance concept“ is grouped
into two categories:
Derived Class (child) - the class that inherits from another class
Base Class (parent) - the class being inherited from
To inherit from a class, use the : symbol.
Example:
class Vehicle // base class (parent)
{
public string brand = "Ford"; // Vehicle field
public void Vehicleprob() // Vehicle method
{
Console.WriteLine(“Check Engine!");
}
}
class Car : Vehicle // derived class (child)
{
public string modelName = "Mustang"; // Car field
23
}
Inheriting and extending classes (2/4)
Example:
class Program
{
static void Main(string[] args)
{
Car myCar = new Car(); // Create a myCar object
// Call the Vehicleprob() method (From the Vehicle class) on the myCar object
myCar.Vehicleprob();
// Display the value of the brand field (from the Vehicle class) and the value
of the modelName from the Car class
Console.WriteLine(myCar.brand + " " + myCar.modelName);
}
}
24
Inheriting and extending classes (3/4)
A C# extension method allows developers to extend the functionality of an existing class
without creating a new one. In that case, the "this" modifier is applied to the first
parameter.
Example:
public class Class1
{
public string Display()
{
return ("I m in Display");
}
30
Method overriding and polymorphism (5/6)
Example:
public class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a shape");
}
}
public class Circle : Shape
{
public override void Draw()
{
// Call the implementation in the base class
base.Draw();
Console.WriteLine("Drawing a circle");
} 31
}
Method overriding and polymorphism (6/6)
Example:
public class Rectangle : Shape
{
public sealed override void Draw()
{
Console.WriteLine("Drawing a rectangle");
}
}
public class Triangle : Rectangle
{
// This will cause a compile-error because the Draw method is sealed in the Rectangle class
public override void Draw()
{
Console.WriteLine("Drawing a triangle");
} 32
}
Access modifiers
By now, we are quite familiar with the public keyword which is used to set the
access level/visibility for classes, fields, methods and properties:
public int number;
Modifier Description
public The code is accessible for all classes
private The code is only accessible within
the same class
protected The code is accessible within the
same class, or in a class that is
inherited from that class.
internal The code is only accessible
within its own assembly, but not from
another assembly.
33
Encapsulation principles
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
1. Declare fields/variables as private
2. Provide public get and set methods, through properties, to access and update the value of a private field.
Example:
public class BankAccount
{
private decimal balance;
//Output
Total Sum : 25
36
Abstract classes and interfaces (3/4)
Another way to achieve abstraction in C#, is with interfaces.
37
Abstract classes and interfaces (4/4)
To implement an interface in C#, you use the : symbol, followed by the name of
the interface. The syntax for implementing an interface is as follows:
class MyClass : MyInterface
{
public void MyMethod()
{
// implementation of MyMethod()
}
public string MyProperty
{
get { return "MyValue"; }
set { }
}
38
}
I3332
3 year Info
rd
Object-Oriented Programming in C#
39