Object Oriented Programming » History » Revision 20
Revision 19 (Amber Herold, 08/02/2011 04:43 PM) → Revision 20/21 (Amber Herold, 08/04/2011 09:21 PM)
h1. Object Oriented Programming
This page provides details of the major features of object oriented programming and definitions of terminology.
h2. Objects
* An _object_ often models a real-world thing - person, animal, shape, car
* Collection of _attributes_ and _behaviors_
* Defined as a _class_ with _member variables_ and _methods_
* An _instance_ of a class is an occurance of an object that has been created using the class _constructor_
* A class _constructor_ method is called automatically when a new instance of a class is created.
* A _destructor_ method is called automatically when an instance of a class is destroyed.
h2. Encapsulation
* Bundle data (attributes) with the methods that operate on the data
* Restrict access to the data using _access modifiers_ like _private_ (only accessable within self) and _protected_ (available to subclasses as well)
* Best Practices
** Keep member variables private
** Provide accessor functions to attributes when needed
** Public methods provide an interface to the rest of the world, everthing else should be private
* Benefit: reduce complexity, increases robustness, by limiting the interdependencies between components.
h2. Inheritance
* Reuse code by basing an object on another object
* Terminology
** Superclass, base class, parent class
** Subclass, derived class, child class
* Examples
** parent: Animal, children: Person, Cat, Fish
** parent: MotorVehicle, children: Car, Truck, Bus, Tractor
** parent: Shape, children: Ellipse, Rectangle, Triangle, Cone
* _Child classes_ inherit attributes and behaviors from _parent classes_
* Child classes may _override_ behaviors inherited from parent classes by providing it's own implementation of a method.
* An _abstract method_ in a parent class does not have an implementation. Child classes MUST provide an implementation to be instantiated.
* An _abstract class_ has at least one abstract method and cannot be instantiated.
* A _virtual method_ in a parent class provides a default implementation that MAY be overriden by the child classes.
h2. Polymorphism
* Objects of different types (or classes) can be defined with the same interface and respond to method calls with the appropriate type-specific behavior.
* The exact type of the object is determined at run-time, so the program does not need to determine type in advance.
* Examples
** 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().
** Make a collection of different animals talk. Code example shown below.
h2. PHP example
<pre>
$animals = array('Lassie'=>'dog', 'Mr. Mistoffelees'=>'cat', 'Missy'=>'cat' );
foreach( $animals as $name=>$type ) {
if ( $type == 'dog' ) {
echo $name.': Woof!';
} else if ( $type == 'cat' ) {
echo $name.': Meow!';
}
}
</pre>
<pre>
interface IAnimal {
function getName();
function talk();
}
abstract class AnimalBase implements IAnimal {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
class Cat extends AnimalBase {
public function talk() {
return 'Meowww!';
}
}
class Dog extends AnimalBase {
public function talk() {
return 'Woof! Woof!';
}
}
$animals = array(
new Cat('Missy'),
new Cat('Mr. Mistoffelees'),
new Dog('Lassie')
);
foreach ($animals as $animal) {
echo $animal->getName() . ': ' . $animal->talk();
}
</pre>
h2. Python Example
<pre>
class Animal:
def __init__(self, name): # Constructor of the class
self.name = name
def talk(self): # Abstract method, defined by convention only
raise NotImplementedError("Subclass must implement abstract method")
class Cat(Animal):
def talk(self):
return 'Meow!'
class Dog(Animal):
def talk(self):
return 'Woof! Woof!'
animals = [Cat('Missy'),
Cat('Mr. Mistoffelees'),
Dog('Lassie')]
for animal in animals:
print animal.name + ': ' + animal.talk()
# prints the following:
#
# Missy: Meow!
# Mr. Mistoffelees: Meow!
# Lassie: Woof! Woof!
</pre>