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