Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.
-
Upload
annabella-jacobs -
Category
Documents
-
view
217 -
download
0
Transcript of Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects.
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.
Memory Heap
Object and Memory
AccountClass
accountObject1
accountObject2Instantiate
Instantiate
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
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?")
Why OOP?
• Inheritance is a major technique for realizing these objectives
Principles of OOP
• Encapsulation• Abstraction• Inheritance• Polymorphism
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).
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
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:
Exercises #1• Sample Interfaceusing System;using System.Collections.Generic;using System.Text;
namespace FileReaderLib{ public class TextReader: IFileReader { }}
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(); }
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.
Example-Abstraction public abstract class FileReaderBase { public abstract void Open(string fileName);
public abstract bool Read();
public abstract void Close();
protected virtual void Initialize() {
} }
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.
Example-Inheritancepublic class MyTextReader: FileReaderBase { public override void Open(string fileName) {}
public override bool Read() { }
public override void Close() {
} }
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.
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) {
}
}