object-oriented programming classes, objects & encapsulation (oh my)
TRANSCRIPT
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.
Examples of ObjectsUse an online store (such as amazon.com) as an
example, the following things may consider as Objects:
Book Customer Order
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.)
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.
• 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
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
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
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!"; }; }