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