Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

19
Object Oriented Programming (OOP)

Transcript of Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Page 1: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Object Oriented Programming (OOP)

Page 2: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

• Let’s discuss first the Classes and Objects

Page 3: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Difference between Class and Object

• A Class is a blue print or a template of an object.

• While an Object is the instance of a class– Note: Instantiate means to Create

• Since there are already existing types such as string, characters, integer and array, A Class defines a new type in the system.

Page 4: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Memory Heap

Object and Memory

AccountClass

accountObject1

accountObject2Instantiate

Instantiate

Page 5: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Overview and Concept

• Object-oriented programming (OOP) is a programming paradigm that uses "objects“– data structures consisting of datafields and methods – and their interactions to design applications and computer programs

Page 6: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Why OOP?• To try to deal with the complexity of the

Program

• To apply principles of abstraction to simplify the tasks of writing, testing, maintaining and understanding complex programs

• To increase code reuse •To reuse classes developed for one application in other applications instead of writing new programs from scratch ("Why reinvent the wheel?")

Page 7: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Why OOP?

• Inheritance is a major technique for realizing these objectives

Page 8: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Principles of OOP

• Encapsulation• Abstraction• Inheritance• Polymorphism

Page 9: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Principles of OOP

• Encapsulation/Information-Hiding– in computer science is the principle of segregation

of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from extensive modification if the design decision is changed. The protection involves providing a stable interface which protects the remainder of the program from the implementation (the details that are most likely to change).

Page 10: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Example-Information-Hiding

• An application the reads several file format

IFileReaderOpen()Read()Close()

File ReaderApplication

TextReaderOpen()Read()Close()

CsvReaderOpen()Read()Close()

ExcelReaderOpen()Read()Close()

Interface

Implement

Implement

Implement

Page 11: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Exercises #1• Code the following example in C#– Create a Library Project, name it as

“FileReaderLib”• Create an Interface “IFileReader”

– Add method:» Void Open(string fileName)» Bool Read()» Void Close()

• Create a new class for the implementation of IFileReader: follow the naming convention on previous slide(i.e TextReader for text Reader)

Sample:

Page 12: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Exercises #1• Sample Interfaceusing System;using System.Collections.Generic;using System.Text;

namespace FileReaderLib{ public class TextReader: IFileReader { }}

Page 13: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Exercises #1• Create a Windows Application Project, name it as

“FileReaderApp”

• Implement the code by referencing FileReaderLib• Sample:

IFileReader _fileReader = new CsvReader();

private void ReadFile(IFileReader fileReader) {

fileReader.Open("my File"); while ( fileReader.Read() ) { } fileReader.Close(); }

Page 14: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Principles of OOP

• AbstractionIn computer science, the mechanism and practice of abstraction reduce and factor out details so that one can focus on a few concepts at a time.

Page 15: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Example-Abstraction public abstract class FileReaderBase { public abstract void Open(string fileName);

public abstract bool Read();

public abstract void Close();

protected virtual void Initialize() {

} }

Page 16: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Principles of OOP

• Inheritance– The ability for a class to extend or override

functionality of another class. The so-called subclass has a whole section that is the superclass and then it has its own set of functions and data.

Page 17: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Example-Inheritancepublic class MyTextReader: FileReaderBase { public override void Open(string fileName) {}

public override bool Read() { }

public override void Close() {

} }

Page 18: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Principles of OOP

• Polymorphism (many form)– is the ability of objects belonging to different

types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior

– “Overloading” a method means your making the method into many forms.

Page 19: Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.

Example-Polymorphismpublic class FileInfo {

private string _fileName;

private int _fileSize;

public FileInfo(string fileName) {

_fileName = fileName;

}

public FileInfo(string fileName, int fileSize) {

_fileName = fileName;

_fileSize = fileSize;

}

public void Open() {

}

public void Open(string fileName) {

}

}