Project

General

Profile

Object Oriented Programming » History » Version 20

Amber Herold, 08/04/2011 09:21 PM

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