Advanced Programming
SUT • Spring 2019
Object Oriented Programming
Characteristics of objects
Interface
Encapsulation
Machine language
Assembly: an abstraction of the machine language
Many languages are abstraction of assembly language
Fortran, Basic, C
Big improvement
But they still require you to think in terms of the structure of the computer
Rather than the structure of the problem
Problem Space
the place where the problem exists
such as a business
Solution Space
the place where you’re implementing that solution such as a computer
The effort required to perform this mapping
Suppose you want to write a library program
What are the elements of your program?
We think about functions and variables…
A step further
Represent problem space elements
This representation is general enough
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”
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
Smalltalk
The first successful object-oriented language
One of the languages upon which Java is based
Java
C++
C##
Elements of OOP
Objects
Message passing between objects
Elements of procedural programming
Functions
Variables
Function invocation
The way of thinking
Thinking about objects and relations
Thinking about functions and computer structure
basic characteristics of Smalltalk:
Everything is an object
A program is a bunch of objects telling each other what to do
by sending messages
Each object has its own memory
made up of other objects
Every object has a type
All objects of a particular type can receive the same messages
An object has state, behavior and identity
Booch added identity to the description
An object (may) have internal data
which gives it state
An object (may) have methods
to produce behavior
And each object can be uniquely distinguished from every other object
Each object has a unique address in memory
Each object can satisfy only certain requests
The requests you can make of an object are defined by its interface
The type is what determines the interface
public class Main {
public static void main(String[] args) {
// Turn On the Light
Light lt = new Light();
lt.on();
}
}
public class Main {
public static void main(String[] args) {
// Person in Education System
Person person = new Person();
person.setName("Taghi Taghavi");
person.setPhoneNumber(66166601L);
person.showInformation();
}
}
Function: Method, Service
Variable: Property, State
Commercial products are encapsulated
Remote control
TV
Cell phone
They are Black Boxes
Hidden Implementations
Public interface
Simplified use
Even for the producer
Open implementation: bad use
Hiding the implementation reduces bugs
It is more beautiful!
Encapsulation of a problem-space concept into a class of objects
Define interface
Hide the implementation
Black box
The client may see the implementation
But can not use it directly
This is better even for the producer (programmer)
Access to some parts of the class is restricted
Public and Private area of the class
The client of a class can use only public area
Public area = class interface
Public methods
Public variables
Lets encapsulate a rectangle
What is a rectangle?
An object
Which has length and width (properties)
Lets you specify its length and width
Can calculate its area and perimeter
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;
}
}
public class Main {
public static void main(String[] args) {
// Rectangle Usage
Rectangle rect = new Rectangle();
rect.setWidth(2);
rect.setLength(7);
System.out.println(rect.calculateArea());
System.out.println(rect.calculatePerimeter());
}
}