Documente Academic
Documente Profesional
Documente Cultură
Introduction to objects
Abstraction
All programming languages provide abstractions The complexity of the problems youre able to solve is directly related to the kind and quality of abstraction ASSEMBLY
.data mesg: .ascii "Hello!\n\0" .text .globl main main: sub $sp, 24 sw $31, 20($sp) la $4, mesg jal printf la $4, mesg jal printf lw $31, 20($sp) add $sp, 24 j $31
IMPERATIVE
int main() { char *msg=Hello!\n; ptintln(msg); } return 0;
OBJECT - ORIENTED
int main() { Message msg; msg.setText(Hello!); msg.print(); } return 0;
All real world's objects are also part of a class of objects that have characteristics and behaviors in common A class describes a set of objects that have identical characteristics and behaviors OOP consists of designing new types (classes), creating variables of these types (instances or objects) and manipulating them (sending messages)
Provides tools to represent elements in the problem space The program is allowed to adapt itself to the lingo of the problem by adding new types of objects When you read the code describing the solution, youre reading words that also express the problem Allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run
Client is the programmer using the class The interface estabilishes what requests you can make for a particular object Hidden implemenattion:
Client
Light
on() off() brighten() dim()
Class Interface
Hidden implementation
Keeps client programmers' hands off portions they shouldn't touch Allows class designer to change the internal working of the class without worrying about how it will affect the client
5
Your new class is made up of any other number of other objects Composition is referred to as a has-a relationship
6
Shape
draw() erase() move() setColor() getColor()
Your new class is a clone (derived) of the original class (base) with additions or modifications Inherintance is referred to as a is-a relationship Two ways to differentiate new derived class from base class:
Circle
Square
It's possible to treat an object as its base type instead of its specific type. Messages sent to this object will be resolved in real time (polymorphism)
7