Project

General

Profile

Object Oriented Programming » History » Version 12

Amber Herold, 08/02/2011 01:08 PM

1 1 Amber Herold
h1. Object Oriented Programming
2
3 3 Amber Herold
h2. Objects
4
5 9 Amber Herold
* An _object_ often models a real-world thing - person, animal, shape, car 
6
* Collection of _attributes_ and _behaviors_
7
* Defined as a _class_ with _member variables_ and _methods_
8
* An instance of a class
9 7 Amber Herold
10
h2. Encapsulation
11
12
13
h2. Inheritance
14
15 10 Amber Herold
* Reuse code by basing an object on another object
16
* Terminology
17 7 Amber Herold
** Superclass, base class, parent class 
18
** Subclass, derived class, child class
19 10 Amber Herold
* Examples
20 1 Amber Herold
** parent: Animal, children: Person, Cat, Fish
21
** parent: MotorVehicle, children: Car, Truck, Bus, Tractor
22
** parent: Shape, children: Ellipse, Rectangle, Triangle, Cone
23
* _Child classes_ inherit attributes and behaviors from _parent classes_
24 9 Amber Herold
* Child classes may _override_ behaviors inherited from parent classes by providing it's own implementation of a method.
25 12 Amber Herold
* An _abstract method_ in a parent class does not have an implementation. Child classes MUST provide an implementation to be instantiated. 
26 9 Amber Herold
* An _abstract class_ has at least one abstract method and cannot be instantiated.
27
* A _virtual method_ in a parent class provides a default implementation that MAY be overriden by the child classes.
28 8 Amber Herold
 
29 1 Amber Herold
30
h2. Polymorhism
31
32
h3. PHP example
33
34
<pre>
35
interface IAnimal {
36
    function getName();
37
    function talk();
38
}
39
 
40
abstract class AnimalBase implements IAnimal {
41
    protected $name;
42
 
43
    public function __construct($name) {
44
        $this->name = $name;
45
    }
46
 
47
    public function getName() {
48
        return $this->name;
49
    }
50
}
51
 
52
class Cat extends AnimalBase {
53
    public function talk() {
54
        return 'Meowww!';
55
    }
56
}
57
 
58
class Dog extends AnimalBase {
59
    public function talk() {
60
        return 'Woof! Woof!';
61
    }
62
}
63
 
64
$animals = array(
65
    new Cat('Missy'),
66
    new Cat('Mr. Mistoffelees'),
67
    new Dog('Lassie')
68
);
69
 
70
foreach ($animals as $animal) {
71
    echo $animal->getName() . ': ' . $animal->talk();
72
}
73
</pre>
74 2 Amber Herold
75
h3. Python Example
76
77
<pre>
78
class Animal:
79
    def __init__(self, name):    # Constructor of the class
80
        self.name = name
81
    def talk(self):              # Abstract method, defined by convention only
82
        raise NotImplementedError("Subclass must implement abstract method")
83
 
84
class Cat(Animal):
85
    def talk(self):
86
        return 'Meow!'
87
 
88
class Dog(Animal):
89
    def talk(self):
90
        return 'Woof! Woof!'
91
 
92
animals = [Cat('Missy'),
93
           Cat('Mr. Mistoffelees'),
94
           Dog('Lassie')]
95
 
96
for animal in animals:
97
    print animal.name + ': ' + animal.talk()
98
 
99
# prints the following:
100
#
101
# Missy: Meow!
102
# Mr. Mistoffelees: Meow!
103
# Lassie: Woof! Woof!
104
</pre>