Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Object Oriented Programming (OOP). Let’s discuss first the Classes and Objects."— Presentation transcript:

1 Object Oriented Programming (OOP)

2 Let’s discuss first the Classes and Objects

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

4 Memory Heap Object and Memory AccountClass accountObject1 accountObject2 Instantiate

5 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 programming paradigmobjects data structuresdatafields methods

6 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?")

7 Why OOP? Inheritance is a major technique for realizing these objectives

8 Principles of OOP Encapsulation Abstraction Inheritance Polymorphism

9 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).computer sciencedesign decisionsinterface

10 Example-Information-Hiding An application the reads several file format IFileReader Open() Read() Close() File Reader Application TextReader Open() Read() Close() CsvReader Open() Read() Close() ExcelReader Open() Read() Close() Interface Implement

11 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:

12 Exercises #1 Sample Interface using System; using System.Collections.Generic; using System.Text; namespace FileReaderLib { public class TextReader: IFileReader { }

13 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(); }

14 Principles of OOP Abstraction In computer science, the mechanism and practice of abstraction reduce and factor out details so that one can focus on a few concepts at a science

15 Example-Abstraction public abstract class FileReaderBase { public abstract void Open(string fileName); public abstract bool Read(); public abstract void Close(); protected virtual void Initialize() { }

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

17 Example-Inheritance public class MyTextReader: FileReaderBase { public override void Open(string fileName) {} public override bool Read() { } public override void Close() { }

18 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 behaviorobjects typesmethod – “Overloading” a method means your making the method into many forms.

19 Example-Polymorphism public 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) { }

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

Similar presentations

Ads by Google