Notes - Lesson 03

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 30

Lesson 03:

Object Oriented
Programming

Objectives
What you will learn...
Object oriented programming paradigm
Bridge imperative programming and OOP practices

Lesson 3: Object Oriented Programming

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.

Lesson 3: Object Oriented Programming

Example of a Class
class myClass {
int num1;
string text;

This is a class with the name


myClass, properties num1
and text, and methods
method1 and method2.

void method1() {
num1 = 10;
}

Fields are global variables


contained in a class.

string method2() {
return text;
}

Methods are simply methods


as discussed in the previous
chapter.

}
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.

Lesson 3: Object Oriented Programming

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.

However, like a variable, we have a variable that called


hold a dog and it is given the name Lassie but there is
no actual dog object that is existing in it. There is no
actual dog, just a variable that can hold a dog.

Lesson 3: Object Oriented Programming

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

Calling Object Methods


Now that we have an instance of a dog, Lassie, we can
now invoke the bark and bite method defined in the class
dog.
Ex. Lassie.bark();
Lassie.bite();
The difference of methods from static ones is that static
methods do not need an instance of the class to invoke
the said method. Depending on the desired function of
the method, there are times when we should and should
not use static methods.
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);

Lesson 3: Object Oriented Programming

A Simple Calculator Class


class calculator {
public int add(int number1, int number2) {
return number1 + number2;
}
public int subtract(int number1, int number2) {
return number1 - number2;
}
public int divide(int number1, int number2) {
return number1 / number2;
}
public int multiply(int number1, int number2) {
return number1 * number2;
}
}
Lesson 3: Object Oriented Programming

10

Calling the Calculator Class


using System;
class caller {
public int num1 = 100;
public int num2 = 5;
static void Main(string[] args) {
Calculator calc = new Calculator();
Console.WriteLine(calc.add(num1, num2));
Console.WriteLine(calc.subtract(num1, num2));
Console.WriteLine(calc.divide(num1, num2));
Console.WriteLine(calc.multiply(num1, num2));
Console.ReadLine();
}
}
Lesson 3: Object Oriented Programming

11

Notes on the Calculator Class


The calculator class and the caller class must be in the
same folder.
The calculator class must be compiled first, followed by
the caller class.
The public keyword before the methods in the calculator
class is an access modifier. Access modifiers will be
discussed in later chapters, for now we will just add that
to our code.

Lesson 3: Object Oriented Programming

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.

Lesson 3: Object Oriented Programming

13

Class Members - Encapsulation


A class is the realization of the concept of encapsulation
in object oriented programming.
Encapsulation is basically putting together related
methods and variables, class members, inside what can
be considered as a container. In our case the container
is the class.
In the calculator class we have an encapsulation of the
add, subtract, divide and multiply methods.
Encapsulation also includes fields but we dont have any
in our calculator class.
Lesson 3: Object Oriented Programming

14

Class Members Types


Methods
Methods in a class are simply methods as we define them
outside of the object oriented programming paradigm.

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

Class Members Types Constructor


Constructors
The concept of constructors is entirely new to object oriented
programming.
Constructors are closely linked to the keyword new. Whenever
we instantiate an object using the new keyword, the class
constructor in the class definition is executed.
Constructors are normally used to initialize, set and instantiate
values of certain fields.
Constructors should be given the public access modifier for them
to be available to instantiations outside the class itself.
Lesson 3: Object Oriented Programming

16

Class Members Constructor cont


Constructors are defined by placing the class name followed by the
parameters placed between the ( and ) symbols then the code
placed between the { and } symbols.
Had we used a constructor in the calculator example, it would look
like this.
Ex. public calculator(){
..; }
Whenever we use the new keyword to instantiate an object we
include the object type followed by the () symbols. Inside the ()
symbols, we can add parameters we want to be provided to the
constructor defined in a class.
For there to be no error, an instantiation calls actual parameter must
match a constructor definitions formal parameter.
Lesson 3: Object Oriented Programming

17

Class Member Constructor Example


class printer {
string message;
public printer(string input) {
message = input;
}
public void print() {
Console.WriteLine(message);
}
}
class caller {
printer p = new printer(Hello);
p.print();
}

Note: Hello is passed as a


parameter to the constructor,
which then assigns it to the
message field.

Lesson 3: Object Oriented Programming

18

What are Access Modifiers?


Access modifiers are keywords that determine the
accessibility of a class member, a class member being
either a property or a method.
Different access modifiers provide different levels of
limiting member access.
Access modifiers are essential in preserving data
integrity since there will be a limit to the accesses of a
member allowed.
By providing stricter access modifiers, we are insured
that there will be less wrongful access.
Lesson 3: Object Oriented Programming

19

Types of Access Modifiers


public
public is the most permissive access modifier. It allows member
access from anywhere that object containing it can be accessed.
Similar to the example above, by instantiating an object of a
given class, we can access all public methods and properties
that object has.
The fallback of the public access modifier is that there are times
we want to control the accessing of our class members, specially
class properties.

Lesson 3: Object Oriented Programming

20

Types of Access Modifiers - Public


class Myclass {
public string hello = hello;
}
To access the field message we only need to have an
instance of the object and we can access it directly.
Myclass M = new Myclass();
string message = M.hello;

Lesson 3: Object Oriented Programming

21

Types of Access Modifiers cont


private
In contrast to public, the private access modifier is the most
restrictive access modifier. It only allows accesses to class
members that come from the class itself.
The class member is more secure since no outside access is
allowed to the class member.
The private access modifier is normally used on important fields
in a class but since the field cannot be accessed directly, there is
a need to create a class method to set or get the values of the
field.

Lesson 3: Object Oriented Programming

22

Types of Access Modifiers - Private


class Myclass {
private string hello = hello;
public string getHello() {
return hello;
}
}
In the example above, since hello is private we cannot access it directly
so we created a public class method that will allow us to retrieve its
value. Note that with only these, there is no way for us to change the
value of hello outside the class itself.
Myclass M = new Myclass();
string message = M.getHello();

Lesson 3: Object Oriented Programming

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

Example of Inheritance cont


In the above example, we defined an interface dog with
a method bark but bark has no implementation. We cant
include implementations in an interface.
We then created two other classes, poodle and bulldog
that have distinct bark methods. Should we not create a
bark method for them there would be an error.
Had we created another class askal and not implement
the bark method there would be a problem but there
would be non should we not only implement the bark
method but also add another method like bite.
Lesson 3: Object Oriented Programming

26

What is an Abstract Class?


An abstract class is very similar to an interface. We need
an implementation of some of its methods before we can
use it.
The difference between an abstract class and an interface
is that abstract classes can contain actual
implementations of its method.
Abstract classes can contain both actual working code as
well as templates therefore we need to make another
class inherit it before we can do instantiating.
To force an implementation of a method, we simply add
the abstract keyword before the methods access modifier.
Lesson 3: Object Oriented Programming

27

Example of Abstract Class


abstract class dog {
void bite() {
; }
public abstract void bark();
}
class bulldog: dog {
public override void bark() {
Console.WriteLine(gggggrrrrr);
}
}
Lesson 3: Object Oriented Programming

28

Example of Abstract Class cont


In the example above, we defined an abstract class dog
which has a method bite and an method bark.
By defining the method bite and not declaring it as
abstract, the inheriting class, the bulldog, already has
that bite method.
Since we declared the method bark as an abstract
method, we are forced to implement the method bark in
the inheriting class bulldog.
Although we only defined the method bark in the bulldog
class, it has both bite and bark as its method.
Lesson 3: Object Oriented Programming

29

Inheritance Protected Access Modifier


Besides the public and private access modifiers we have
the protected access modifier.
Class members with the protected access modifier are
accessible from within the class itself as well as from any
class that inherits it.
Suppose a class B inherits class A, then all class
members declared to be protected in A can still be
accessed by creating an instance of class A in class B.

Lesson 3: Object Oriented Programming

30

You might also like