object-oriented programming classes, objects & encapsulation (oh my)

9
Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Upload: christal-webb

Post on 02-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Object-Oriented Programming

Classes, Objects & Encapsulation

(oh my)

Page 2: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

I’m not real

What is Object Orientation?

• Object orientation is a technique for system modeling

• Modeling tries to simulate real life

• Objects within a system interact with each other (hence ERDs) eg. Object Student could be expected to deliver their homework to Object Teacher who will mark it.

• All objects have attributes (characteristics), this is sometimes referred to as state.

• They also have methods (or functions), this is sometimes referred to as behavior.

Page 3: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Examples of ObjectsUse an online store (such as amazon.com) as an

example, the following things may consider as Objects:

Book Customer Order

Page 4: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Object Attributes and Operations

• Attributes know something

• Attributes are often data, like order ID.

• Attributes can also be another object, such as the entire Customer object rather than just the customer ID.

• Methods do something with what the attributes know, (Methods can be actions that the object does, often affecting its attributes.)

Page 5: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Difference Between Object And Class

• A class is how you define an object. Classes are descriptive categories or templates. Book could be a class.

• Objects are unique instances of classes. This Java Certification book that costs $59.99 with item ID 62467-B is an object of the Book class.

Page 6: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

• The attributes and methods defined by a class are for its objects, not for itself.

• There is no actual Book, but there are Book objects

• The class is the logical concept of a Book

• The object is Great Expectations

Page 7: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Encapsulation• Encapsulation is the mechanism that binds together

the code and the data it manipulates, and keeps both safe from outside interference and misuse.

A Class

Private variables and methods

Public variables and methods

Public variables is not recommended

Page 8: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Inheritance• Inheritance is the process by which one object

acquires the attributes of another object. By use of inheritance, an object need only define all of its attributes that make it unique within its class, it can inherit its general attributes from its parent.

Account

Current Savings Loan

Page 9: Object-Oriented Programming Classes, Objects & Encapsulation (oh my)

Polymorphism• Polymorphism(from Greek, meaning “many forms”)

function Animal(name) { this.name = name; this.talk = function () { return ""; }; } function Cat(name) { Animal.call(this, name); this.talk = function() { return "Meow!!!"; }; } function Dog(name) { Animal.call(this, name); this.talk = function() { return "Woof! Woof!"; }; }