Project

General

Profile

Object Oriented Programming » History » Version 15

Amber Herold, 08/02/2011 01:42 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 15 Amber Herold
* An _instance_ of a class is an occurance of an object that has been created using the class _constructor_ 
9
* A class _constructor_ method is called automatically when a new instance of a class is created. 
10
* A _destructor_ method is called automatically when an instance of a class is destroyed.
11 7 Amber Herold
12
h2. Encapsulation
13
14 13 Amber Herold
* Bundle data (attributes) with the methods that operate on the data
15
* Restrict access to the data using _access modifiers_ like private and protected
16
* Best Practices 
17
** Keep member variables private
18
** Provide accessor functions to attributes when needed
19
** Public methods provide an interface to the rest of the world, everthing else should be private
20 14 Amber Herold
* Benefit: reduce complexity, increases robustness, by limiting the interdependencies between components.
21 7 Amber Herold
22
h2. Inheritance
23
24 10 Amber Herold
* Reuse code by basing an object on another object
25
* Terminology
26 7 Amber Herold
** Superclass, base class, parent class 
27
** Subclass, derived class, child class
28 10 Amber Herold
* Examples
29 1 Amber Herold
** parent: Animal, children: Person, Cat, Fish
30
** parent: MotorVehicle, children: Car, Truck, Bus, Tractor
31
** parent: Shape, children: Ellipse, Rectangle, Triangle, Cone
32
* _Child classes_ inherit attributes and behaviors from _parent classes_
33 9 Amber Herold
* Child classes may _override_ behaviors inherited from parent classes by providing it's own implementation of a method.
34 12 Amber Herold
* An _abstract method_ in a parent class does not have an implementation. Child classes MUST provide an implementation to be instantiated. 
35 9 Amber Herold
* An _abstract class_ has at least one abstract method and cannot be instantiated.
36
* A _virtual method_ in a parent class provides a default implementation that MAY be overriden by the child classes.
37 8 Amber Herold
 
38 1 Amber Herold
39
h2. Polymorhism
40
41 15 Amber Herold
* Objects of different types (or classes) can be defined with the same interface and respond to method calls with the appropriate type-specific behavior.
42
* The exact type of the object is determined at run-time, so the program does not need to determine type in advance.
43
* Examples 
44
** Draw every shape in a collection of shapes. The collection has Rectangles and Triangles, both based on the Shape class, which has an abstract method called Draw().
45
** Make a collection of different animals talk. Code example shown below.
46 1 Amber Herold
47 15 Amber Herold
h2. PHP example
48
49 1 Amber Herold
<pre>
50
interface IAnimal {
51
    function getName();
52
    function talk();
53
}
54
 
55
abstract class AnimalBase implements IAnimal {
56
    protected $name;
57
 
58
    public function __construct($name) {
59
        $this->name = $name;
60
    }
61
 
62
    public function getName() {
63
        return $this->name;
64
    }
65
}
66
 
67
class Cat extends AnimalBase {
68
    public function talk() {
69
        return 'Meowww!';
70
    }
71
}
72
 
73
class Dog extends AnimalBase {
74
    public function talk() {
75
        return 'Woof! Woof!';
76
    }
77
}
78
 
79
$animals = array(
80
    new Cat('Missy'),
81
    new Cat('Mr. Mistoffelees'),
82
    new Dog('Lassie')
83
);
84
 
85
foreach ($animals as $animal) {
86
    echo $animal->getName() . ': ' . $animal->talk();
87
}
88
</pre>
89 2 Amber Herold
90 15 Amber Herold
h2. Python Example
91 2 Amber Herold
92
<pre>
93
class Animal:
94
    def __init__(self, name):    # Constructor of the class
95
        self.name = name
96
    def talk(self):              # Abstract method, defined by convention only
97
        raise NotImplementedError("Subclass must implement abstract method")
98
 
99
class Cat(Animal):
100
    def talk(self):
101
        return 'Meow!'
102
 
103
class Dog(Animal):
104
    def talk(self):
105
        return 'Woof! Woof!'
106
 
107
animals = [Cat('Missy'),
108
           Cat('Mr. Mistoffelees'),
109
           Dog('Lassie')]
110
 
111
for animal in animals:
112
    print animal.name + ': ' + animal.talk()
113
 
114
# prints the following:
115
#
116
# Missy: Meow!
117
# Mr. Mistoffelees: Meow!
118
# Lassie: Woof! Woof!
119
</pre>