Professional Documents
Culture Documents
Oo 1
Oo 1
Oo 1
Decomposition
Abstraction types
Specifications
Contract
Mutability
Invariants
Operation types
Encapsulation
javaprogrammering
Decomposition
Complex problem Strategy: ”Divide and conquer!”
javaprogrammering
Hotel
hotel
floor
room
facilities
document
reservation
receipt
person
guest
personnel
clerk
cleaning staff
chef
javaprogrammering
Abstraction
• Abstract – “to consider apart from application to a
particular instance” (Webster’s Dictionary)
javaprogrammering
General abstraction types
• Abstraction by parameterization – abstract from specific
identifiers to parameters; allows for general use (reuse) (cf.
actual vs formal parameters)
javaprogrammering
Abstraction types
• Procedural abstractions (functional abstractions) –
operations, e g sort(), sqrt(), abs()
javaprogrammering
Why abstractions?
• Easy to understand – you only need to understand what a
method does, not how it does it
javaprogrammering
Procedural abstraction, ex 1
Repeated code; different identifiers but same function:
if (a<0) a=(-a);
.
.
if (varB<0) varB=(-varB);
.
.
if (anotherVar<0) anotherVar=(-anotherVar);
.
javaprogrammering
Procedural abstraction, ex 1 (cont)
/** Returns the absolute value of p */
public static int abs(int p) {
int i=p;
if (i<0) {
i=(-i);
}
return i;
}
javaprogrammering
Procedural abstraction, ex 1 (cont)
a = abs(a);
.
.
varB = abs(varB);
.
.
anotherVar = abs(anotherVar);
.
javaprogrammering
Procedural abstraction, ex 1 (cont)
Abstraction by
specification
javaprogrammering
Procedural abstraction, ex 2
Abstraction by
specification
Abstraction by
parameterization
javaprogrammering
Contract
Contract – often used to describe procedural
abstractions (but also e g interfaces).
• Pre-conditions
• Post-conditions
javaprogrammering
Specifications for procedures
• Description of abstraction
• Can contain:
– What is required? (e.g. non-null input)
– What is returned? (if any…)
– What is modified? (any inputs changed?)
– What is the behaviour? (assuming requirements are
satisfied)
• Usually informal
• Use javadoc (comment before method definition)
javaprogrammering
Specifications for procedures
• Too specific constrains the implementor (e.g. an id-
number must comply with a certain format)
javaprogrammering
Data abstraction
• Fundamental in OO-programming!
javaprogrammering
Abstraction function, class invariant
• Abstraction function (AF) – formal description of the
relationship between an abstraction and the (typical)
internal representation of its implementation
javaprogrammering
Specifications for data abstractions
• Conceptual description
• Mutable or immutable?
javaprogrammering
Specification, class Person
/** This class represents a person with name, address and an id-number.
All but the id-number can be modified through the provided set-
methods.*/
public class Person {
/** The id is unique and cannot be changed. */
private int id;
.
.
/** Sets the name of this person. If the provided string is non-valid (null
or empty) a NonValidNameException will be thrown. */
public void setName(String s) throws NonValidNameException {
…
}
.
}
javaprogrammering
Operation types
• Creators – produces new objects “from scratch” (most
constructors)
javaprogrammering
public class Person{
class Person, ex
private String name;
private String addr;
private String id;
public Person(String n, String a, String i) {
name = n;
addr = a;
id = i; constructor
} observer
public String getName(){
return name; mutator
}
public void setName(String n) throws NonValidNameException{
if (n!=null && n.length()>0) name = n;
else throw new NonValidNameException();
}
producer
public Person createChild(String n, String i) {
…
}
.
. javaprogrammering
Mutability
• Mutable – data can be changed
javaprogrammering
Mutable or immutable?
• Would the object naturally have changing values?
• Immutable is safer
javaprogrammering
public class Employee{
Encapsulation, ex
private int salary;
public Employee(int s) { salary = s; }
public setSalary(int s) throws NonValidSalaryException {
if(s>0 && s <= 40000){
salary = s;
} else throw new NonValidSalaryException();
}
}
.
.
Employee e = new Empoyee(20000);
e.salary = -10000; // not allowed! (compile error)
e.setSalary(-10000); // will throw exception (at runtime)
e.setSalary(10000); // works!
.
.
javaprogrammering
Encapsulation
Make the data unreachable from outside – access only via
public methods (a public interface).
javaprogrammering
Exposing the representation
public MyArrayContainer {
private int[] iArr;
public MyArrayContainer(int[] a) {
iArr = a;
}
public int getElement(int indx) {
return iArr[indx];
}
public int[] getAllElements() {
return iArr;
} One constructor and two
} observers, but no mutators…
- Is it immutable?
javaprogrammering
How many operations?
• A “fully populated” type has operations sufficient to obtain
every possible state.
javaprogrammering
MODULE: Inheritance
• Polymorphism
• Access modifiers
• Casting
• Constructors
• Overriding
• this and super
• Interface
• static
javaprogrammering
Abstraction hierarchy
Person
Customer Employee
javaprogrammering
class Person{
Inheritance, ex
String name;
String addr;
}
javaprogrammering
class Car{
String regNo;
Vehicle, ex
int fuelConsumption;
int maxLoad;
int noOfPassengers;
}
class Motorcycle{
String regNo;
int fuelConsumption;
int noOfPassengers;
}
class Aeroplane{
int fuelConsumption;
int maxLoad;
int noOfPassengers;
}
javaprogrammering
class Vehicle{
Vehicle, ex (cont)
int fuelConsumption;
int maxLoad;
int noOfPassengers;
}
javaprogrammering
”Is-a” relation
Vehicle
fuelConsumption:int
maxLoad:int
noOfPassengers:int
RegisterableVehicle
Aeroplane regNo:String
Motorcycle Car
javaprogrammering
Heterogeneous collections
public class HeterogeneousCollection {
Vehicle[] myVehicles = {
new Car(),
new Motorcycle(),
new Aeroplane() };
}
javaprogrammering
This document was created with Win2PDF available at http://www.daneprairie.com.
The unregistered version of Win2PDF is for evaluation or non-commercial use only.