Object Orientation An Object oriented approach views systems and programs as a collection of...

15

Transcript of Object Orientation An Object oriented approach views systems and programs as a collection of...

Page 1: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.
Page 2: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

Object OrientationAn Object oriented approach views systems

and programs as a collection of interacting objects.

An object is a thing in a computer system that is capable of responding to messages

Page 3: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

The Idea of OOPThe idea of OOP is to try to approach

programming in a more natural way by grouping all the code that belongs to a particular object—such as a checking account or a customer—together

Page 4: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

ObjectsCore to the idea of OOPs is the concept of an

object.An object is anything that is relevant to your

programA customer, an employee, Inventory, a

database, a button, a form, a sale are all potential objects

Page 5: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

Benefits of ObjectsMore natural way to look at thingsRe-usability

Page 6: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

Objects/ClassesA class is a description of an

object. This description can include

attributes which describe the classIt can also include “methods”

which describe things the object can do.

In programming an object is an actual instance of a class

Page 7: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

MessagesObject communicate among themselves by

means of messagesThe also maintain “associative relationships”

among themselves

Page 8: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

A Class Diagram

Class name

Field names

methods

- Means private

+Means Public

Page 9: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

Login Class in C#class Login { //constructor public Login(string usr, string pass) { Username = usr; Password = pass; Authenticate(); } //private field variables private string username;

//public property public string Username { get { return username; } set { username = value; } }

private string password;

//write only property public string Password { set { password = value; } }

private int Authenticate() { //connect to database etc . . . int valid = 0; if (Username && Password) { valid = 1; } return valid; } }

Page 10: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

Principles of OOPAbstractionEncapsulation InheritancePolymorphism

Page 11: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

AbstractionThe idea of abstraction is that a class

represents an “abstract” version of an objectA customer class presents the abstract idea

of what a customer isA sale class represents an abstract idea of

what a sale is

Page 12: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

EncapsulationEncapsulation refers to the idea that a class

should contain all the properties and methods of an object

It also means you should be able to use the object without knowing the details of how it is structured internally

Page 13: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

InheritanceInheritance means you can derive a new

object from an existing oneIt also means that the new object will have

access to (will inherit) the properties and methods from the parent object

Page 14: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

InheritanceEmployee

Salary Hourly Contract

Generalization/ Specialization

Page 15: Object Orientation An Object oriented approach views systems and programs as a collection of interacting objects. An object is a thing in a computer system.

PolymorphismPolymorphism means that objects descended

from a particular types will behave appropriately

For example (a listbox and a button are both descended from the same class called control—but they each will behave differently)