Object Oriented Programming » History » Version 19
Amber Herold, 08/02/2011 04:43 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 | interface IAnimal { |
||
53 | function getName(); |
||
54 | function talk(); |
||
55 | } |
||
56 | |||
57 | abstract class AnimalBase implements IAnimal { |
||
58 | protected $name; |
||
59 | |||
60 | public function __construct($name) { |
||
61 | $this->name = $name; |
||
62 | } |
||
63 | |||
64 | public function getName() { |
||
65 | return $this->name; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | class Cat extends AnimalBase { |
||
70 | public function talk() { |
||
71 | return 'Meowww!'; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | class Dog extends AnimalBase { |
||
76 | public function talk() { |
||
77 | return 'Woof! Woof!'; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | $animals = array( |
||
82 | new Cat('Missy'), |
||
83 | new Cat('Mr. Mistoffelees'), |
||
84 | new Dog('Lassie') |
||
85 | ); |
||
86 | |||
87 | foreach ($animals as $animal) { |
||
88 | echo $animal->getName() . ': ' . $animal->talk(); |
||
89 | } |
||
90 | </pre> |
||
91 | 2 | Amber Herold | |
92 | 15 | Amber Herold | h2. Python Example |
93 | 2 | Amber Herold | |
94 | <pre> |
||
95 | class Animal: |
||
96 | def __init__(self, name): # Constructor of the class |
||
97 | self.name = name |
||
98 | def talk(self): # Abstract method, defined by convention only |
||
99 | raise NotImplementedError("Subclass must implement abstract method") |
||
100 | |||
101 | class Cat(Animal): |
||
102 | def talk(self): |
||
103 | return 'Meow!' |
||
104 | |||
105 | class Dog(Animal): |
||
106 | def talk(self): |
||
107 | return 'Woof! Woof!' |
||
108 | |||
109 | animals = [Cat('Missy'), |
||
110 | Cat('Mr. Mistoffelees'), |
||
111 | Dog('Lassie')] |
||
112 | |||
113 | for animal in animals: |
||
114 | print animal.name + ': ' + animal.talk() |
||
115 | |||
116 | # prints the following: |
||
117 | # |
||
118 | # Missy: Meow! |
||
119 | # Mr. Mistoffelees: Meow! |
||
120 | # Lassie: Woof! Woof! |
||
121 | </pre> |