Upload
leon-doyle
View
212
Download
0
Embed Size (px)
Citation preview
Advanced Programming in Java
Presented by: Mojtaba Khezrian
Sharif University of Technology 4
Different ContextsProblem Space
the place where the problem existssuch as a business
Solution Spacethe place where you’re implementing that
solutionsuch as a computer
The effort required to perform this mapping
Fall 2014
Sharif University of Technology 6
Library ProblemSuppose you want to write a library programWhat are the elements of your program?We think about functions and variables…
Fall 2014
Sharif University of Technology 7
Object Oriented ApproachOO approach goes a step further Lets the programmer represent problem
space elementsThe programmer is not constrained to any
particular type of problem.
The elements in the problem space and their representations in the solution space are referred to as “objects”
Fall 2014
Sharif University of Technology 8
OOP
The program is allowed to adapt itself to the lingo of the problem by adding new types of objects
when you read the code, you’re reading words that also express the problem.
This is a more flexible and powerful language abstraction
Fall 2014
Sharif University of Technology 9
OOP (2)
OOP allows you to describe the problem in terms of the problem
Rather than in terms of the computer Objects in your code are similar to real
objects
Recall the sample programs: phonebook and library
Fall 2014
Sharif University of Technology 11
OOP vs. Procedural ApproachElements of procedural programming
Functions VariablesFunction invocation
Elements of OOPObjectsMessage passing between objects
The way of thinkingThinking about functions and computer structureThinking about objects and relations
Fall 2014
Sharif University of Technology 12
OOP Characteristics
1. Everything is an object 2. A program is a bunch of objects telling each
other what to do by sending messages
3. Each object has its own memory made up of other objects
4. Every object has a type 5. All objects of a particular type can receive the
same messages Fall 2014
Sharif University of Technology 13
Everything is an object You can take any conceptual component in the
problem dogs, buildings, books, people, …
And represent it as an object in your program. Example
Person p; Book book;
Think of an object as a variableIt stores dataBut you can make requests to that object
asking it to perform operations on itself. Fall 2014
Sharif University of Technology 14
Object MessagesTo make a request of an object, you send a
message to that object. Message = invoking a method of an object. Example
Book b;….if(b.isReserved())…
Person p;….p.setPhoneNumber(66166601)
Fall 2014
Sharif University of Technology 15
Each object has its own memory
You can create a new kind of object by making a package containing existing objects
Thus, you can build complexity into a program while hiding it behind the simplicity of objects
Book{String name;Person reservedTo;
}
Fall 2014
Sharif University of Technology 16
Every object has a typeEach object is an instance of a classclass is synonymous with type The most important distinguishing
characteristic of a class is What messages can you send to it?
Person p;Person q;Person[] people;
Fall 2014
Sharif University of Technology 17
SubstitutabilityAll objects of a particular type can receive the same
messagesAn object of type circle is also an object of type
shapeA circle is guaranteed to accept shape messages You can write code that talks to shapes and
automatically handle anything that fits the description of a shape
This substitutability is one of the powerful concepts in OOP. InheritancePolymorphism
Fall 2014
Sharif University of Technology 18
Description of an ObjectAn object has state and behaviorAn object (may) have internal data
which gives it stateAn object (may) have methods
to produce behaviorAnd each object can be uniquely
distinguished from every other objectEach object has a unique address in memory
Fall 2014
Sharif University of Technology 19
Abstract Data TypesEach programming language has some
predefined data typesint, double, char, …
Creating abstract data types is a fundamental concept in object-oriented programming
Abstract Data Type = ClassProgrammer defines a class to fit a problem You extend the programming language by
adding new data types specific to your needs
Fall 2014
Sharif University of Technology 20
MessagesE.g.
Assign a book to a personSet phone number of a personCalculate area of a shapecomplete a transactiondraw something on the screenturn on a switch
Fall 2014
Sharif University of Technology 21
InterfaceEach object can satisfy only certain requestsThe requests you can make of an object are
defined by its interfaceThe type is what determines the interface
Fall 2014
Sharif University of Technology 22
Representation of a light bulb:
Fall 2014
UML Diagram
Sharif University of Technology 23
Person in an Education System
Fall 2014
Sharif University of Technology 24
New names in OOP
Function Method, ServiceVariable Property, State
Fall 2014
Sharif University of Technology 25
EncapsulationCommercial products are encapsulated
Remote controlTVCell phone
They are Black BoxesHidden ImplementationsPublic interface
Fall 2014
Sharif University of Technology 26
Why Encapsulation?Simplified use
Even for the producerOpen implementation bad useHiding the implementation reduces bugsIt is more beautiful!
Fall 2014
Sharif University of Technology 27
Object EncapsulationEncapsulation of a problem-space concept
into a class of objectsDefine interfaceHide the implementationBlack boxThe client may see the implementationBut can not use it directlyThis is better even for the producer
(programmer)
Fall 2014
Sharif University of Technology 28
Access ControlAccess to some parts of the class is restrictedPublic and Private area of the classThe client of a class can use only public areaPublic area = class interface
Public methodsPublic variables
Fall 2014
Sharif University of Technology 29
Example: RectangleLets encapsulate a rectangleWhat is a rectangle?
An objectWhich has length and width (properties)Lets you specify its length and widthCan calculate its area and perimeter
Fall 2014
public class Rectangle {
private int width, length;
public void setWidth(int w) {width = w;
}public void setLength(int l) {
length = l;}public int calculateArea(){
return width*length;}public int calculatePerimeter(){
return (width+length)*2;}
}Fall 2014
Private area: hidden implementation
Public area :the interface
Class Declaration
Sharif University of Technology 31
How to Use Rectangle?
Rectangle rect = new Rectangle();rect.setWidth(2);rect.setLength(7);System.out.println(rect.calculateArea());System.out.println(rect.calculatePerimeter());
Fall 2014
Object creation or instantiation
Sharif University of Technology 32
More ExercisesEncapsulate these concepts:
StudentFootbal-PlayerDog
name, age : properties bite, bark : messages (actions)
Fall 2014
Sharif University of Technology 33
ConclusionOOP brings us a new abstractionOOP allows you to describe the problem in
terms of the problemThink in terms of the structure of the
problemRather than the structure of the computer
Object has state, behavior and identityObject has data and interface
Fall 2014
Sharif University of Technology 34
Further ReadingGoogle these queries and read some pages
(Wikipedia is preferred)Object Oriented ProgrammingHistory of Object Oriented ProgrammingSmalltalkUMLEncapsulationInheritancePolymorphism
Fall 2014