2009-Mar-7 - OOP languages and concepts
OOP concepts:
- encapsulation - Conceals the functional details of a class*.
- modularity - Simple parts connected by clean interface.
- polymorphism** - Different data types respond to methods of same name
- inheritance - Subclass, specialized version of class.
*Class - abstract characteristics of a thing (object)
Object - pattern of a class
Instance - actual object created at runtime
Multiple inheritance - one subclass has more than one superclass
**Polymorphism:
- Override - Child-class overwrites parent-class function (NOT private)
- Overload - Functions with same name, but different parameters (type / number / order) (NOT private in parent)
If a function would have different behavior in subclasses, it should be virtual.
If a superclass (base class) has a virtual function, it should be overridden.
OOP languages: C++, Java, Python, Perl ...
| |
C++/Java |
Python |
Perl |
| integers |
primitive native types |
objects (of int class) |
treated as double float |
| self pointer |
this |
self |
(any) |
| constructor |
classname() |
__init__ |
NEW |
| destructor |
~classname() |
__del__ |
DESTROY |
| class members |
public, private, protected |
all public |
all public |
| methods** |
public, private, protected |
all virtual |
all virtual |
| private variable |
private xxx; |
__privatevar |
all public* |
| pass by value/reference |
C passes by value; Java passes references to objects by value. |
Passes references to objects by value.*** |
Pass by reference - hard / symbolic |
* Perl coding convention: use _ or __ (double underscore) before variable to indicate private. But not enforced.
** Subroutine:
- function: "return xx;"
- procedure: no return. Sometimes synonymous with function, eg. in C.
- method: OOP concept, part of objects
*** Python 2 kinds of data structure:
Immutable: string, tuple
Mutable: list, dictionary
|