Professional Documents
Culture Documents
Notes - Lesson 03
Notes - Lesson 03
Notes - Lesson 03
Object Oriented
Programming
Objectives
What you will learn...
Object oriented programming paradigm
Bridge imperative programming and OOP practices
What is a class?
A class is a blueprint of an object. It defines all methods
and properties of the said object.
All codes done in C# are placed in a class.
A class is given a name using the class keyword and the
code that follows is called the class definition.
The class definition is placed inside the { and } symbols.
Example of a Class
class myClass {
int num1;
string text;
void method1() {
num1 = 10;
}
string method2() {
return text;
}
}
Lesson 3: Object Oriented Programming
Objects in a Nutshell
Objects are the output of out classes. They are the
actual instance of these classes.
As a simple analogy, for instance we have a class dog
which has properties color and breed, methods bark and
bite. An example of an object is Lassie, an instance of
the dog class which can both bark and bite and has a
specific color or breed.
In order for us to use our classes, we must have an
instance of it, an object defined by it.
Declaring Objects
Objects can be treated similarly to how as variables are
treated. Class that would hold its definition would be the
data type and it should be followed by a name.
Ex. dog Lassie;
Note: This is assuming we have a class dog.
Instantiation
Unlike variables wherein we simply assign a value to it,
the only way to use an object would to create an
instance of it.
In the previous example, for us to have Lassie, a dog
that can both bark and bite, we first have to create an
instance of a dog given the name Lassie.
Instantiation is done using the new keyword.
Ex. dog Lassie = new dog();
cat Garfield = new cat();
Lesson 3: Object Oriented Programming
Object Properties
In the example, we have an instance of a dog, Lassie. In
the class definition of a dog there is a property of color
and breed.
These properties can be generally treated as regular
variables except that in accessing them we both the
object name and the property name.
Ex. Lassie.color = brown;
Console.WriteLine(Lassie.breed);
10
11
12
Class Members
Class members are the things we see defined in a
class.
In the example above, we saw a simple class but only
the most basic of the class members are found here,
class methods.
Apart from methods, other basic class members include
constants, fields, and constructors.
13
14
Fields
Fields refer to variables declared in the class definition level. In
the caller class example, the variables num1 and num2 are
considered to be fields.
However, the different number1 and number2 variables in the
calculator class are not fields since they are contained in a
method.
Constants
Similar to fields constants are variables declared in the class
definition level, only that they are also defined as constants.
Lesson 3: Object Oriented Programming
15
16
17
18
19
20
21
22
23
Inheritance
An issue to the problem above would be the concept of
interfaces. Suppose we have a general class dog, then
can we not make another class, say bulldog whose bark
we can define another way but should still be able to bite,
bark and have color.
Inheritance is creating another class that inherits all the
methods with their matching parameters and forcing you
to implement these.
It is mainly used to create modularize yet compatible
works by ensuring that everyone doing a task follows the
discussed format.
Lesson 3: Object Oriented Programming
24
Example of Inheritance
interface dog {
void bark();
}
class bulldog: dog {
public void bark(){
Console.WriteLine(gggrraaafff);
}
}
class poodle: dog {
public void bark() {
Console.WriteLine(waff);
}
}
Lesson 3: Object Oriented Programming
25
26
27
28
29
30