Introduction To Object-Oriented Programming PDF
Introduction To Object-Oriented Programming PDF
OOP: Introduction 1
Objects and Classes
Mammal
Two-legs
Very large brains
Omnivorous (plants + meat)
Mammal
Tusks
Four legs
Herbivorous (plant eater)
OOP: Introduction 2
The Object Concept
An object is an encapsulation of data.
An object has
identity (a unique reference)
social security number (cpr), employee number, passport number
state, also called characteristics (variables)
hungry, sad, drunk, running, alive
behavior (methods)
eat, drink, wave, smile, kiss
OOP: Introduction 3
The Class Concept
A class is a collection of objects (or values) and a
corresponding set of methods.
A class encapsulates the data representation and makes data
access possible at a higher level of abstraction.
OOP: Introduction 4
Encapsulation and Information Hiding
Data can be encapsulated such that it is invisible to the
outside world.
Data can only be accessed via methods.
send
Data Data
message
Function Method
Function Method
Function Method
Procedural Class
OOP: Introduction 5
Encapsulation and Information Hiding, cont.
What the outside world cannot see it cannot depend on!
The object is a fire-wall between the object and the outside
world.
The hidden data and methods can be changed without
affecting the outside world.
Outside world
Client interface
OOP: Introduction 7
Connection between Object and Class
In object-oriented programming we write classes
The text files we create contain classes!
Static
One
Objects are created from classes
A class contains a receipe on how to make objects
Dynamic
Many
Ingrediens
250 g digestive biscuits food processor
125 g soft brown sugar saucepan
125 g butter wooden spoon
50 g raisins 18 cm sandwich tin (greased)
3 tablespoons cocoa powder fridge
1 egg, beaten knife
25 g = 1 oz
2.5 cm = 1 inch
Process
blend
bake
source http://www.filflora.com
OOP: Introduction source http://www.icbl.hw.ac.uk/ltdi/cookbook/chocolate_cake/ 8
Type and Interface
An object has type and an interface.
Account Type
balance()
withdraw() Interface
deposit()
OOP: Introduction 9
Instantiating Classes
An instantiation is a mechanism where objects are created
from a class.
Always involves storage allocation for the object.
A mechanism where objects are given an initial state.
OOP: Introduction 10
Interaction between Objects
Interaction between objects happens by messages being send.
A message activates a method on the calling object.
message
O1 O2
message
message O3
OOP: Introduction 11
Phenomenon and Concept
A phenomenon is a thing in the real world that has
individual existence.
an object
A concept is a generalization, derived from a set of
phenomena and based on the common properties of these
phenomena.
a class
Characteristics of a concept
A name
Intension, the set of properties of the phenomenon
Extension, the set of phenomena covered by the concept.
OOP: Introduction 12
Classification and Exemplification, Examples
hat, 23, 34, mouse, telephone, book, 98, 45.34, hello
numbers: 23, 34, 98, 45.34
words: hat, mouse, telephone, book, hello
OOP: Introduction 13
Classification and Exemplification, cont.
A classification is a description of which phenomena that
belongs to a concept.
An exemplification is a phenomenon that covers the concept
Concept
classification exemplification
Phenomenon
OOP: Introduction 14
Aggregation and Decomposition, Example
Idea: make new objects by combining existing objects.
Reusing the implementation!
Engine
start() Car
stop()
Engine
Gearbox Gearbox
Aggregation Doors[4]
up()
down()
start()
Door drive()
open()
close() new class
existing classes
OOP: Introduction 16
Generalization and Specialization
Concept B Concept D
Vehicle
Car Truck
OOP: Introduction 18
Generalization and Specialization, Example
Inheritance: get the interface from the general class.
Objects related by inheritance are all of the same type.
Shape
draw()
resize()
Square
draw()
resize()
Shape
draw()
resize()
OOP: Introduction 20
Polymorphism and Dynamic Binding
void doSomething(Shape s){
s.draw(); // magically calls the specific class
s.resize();
}
Circle c = new Circle();
Line l = new Line();
Rectangle r = new Rectangle();
OOP: Introduction 21
Benefit Generalization and Specialization
Take previous Shape class hierarchy
remove inheritance
remove general and abstract class Shape
Rectangle Square
draw() draw()
resize() resize()
Circle Line
draw() draw()
resize() resize()
OOP: Introduction 22
Code Example, Revisited
void doSomething(Circle c){ void doSomething(Rectangle r){
c.draw(); r.draw();
c.resize(); r.resize();
} }
void doSomething(Line l){ void doSomething(Square s){
l.draw(); s.draw();
l.resize(); s.resize();
} }
OOP: Introduction 23
Java Program Structure
// comment on the class
public class MyProg {
String s = Viggo; variable
OOP: Introduction 24
Java Class Example Car
/** A simple class modeling a car. */
public class Car {
// instance variables
private String make;
private String model;
private double price;
// constructor
public Car(String m, String mo, double p) {
make = m; model = mo; price = p;
}
// string representation of the car
public String toString() {
return "make: " + make + " model: "
+ model + " price: " + price;
}
}
OOP: Introduction 25
Byte Code vs. Executable
MyProg.java MyProg.cpp
javac MyProg.java
gcc MyProg.cpp
-o myprog.exe
Java Class File
MyProg.class
Portable Byte Code
OOP: Introduction 29
Common Mistakes and Errors
// what is ugly here?
public class main {
public static void main(String[] args){
System.out.println(Hello World);}
}
// what is wrong here?
public class MyClass {
public void static main(string[] args){
system.out.println(Hello World);}
}
OOP: Introduction 30
Structuring by Program or Data?
What are the actions of the program vs. which data does the
program act on.
Top-down: Stepwise program refinement
Bottom-up: Focus on the stable data parts then add methods
OOP: Introduction 31
Pure Object-Oriented Languages
Five rules [source: Alan Kay]
Everything in an object.
A program is a set of objects telling each other what to do by
sending messages.
Each object has its own memory (made up by other objects).
Every object has a type.
All objects of a specific type can receive the same messages.
OOP: Introduction 32