Tutorial - INTRODUCTION TO OOP
1 * A Class inherits from another class or acts as
original class.(1/1)
2 * Descendant Class is always greater than or equal
to its' ancestor (1/2)
3 * Object can consist of two elements (Data and Method)
(1/3)
4 * Any deviation from ancestor class causes polymorphism
(1/4)
5 * Method of the descendant class with the same name
will replace its ancestor's Method. (1/5)
6 * Method of the descendant class can override and
inherit in part or in full from its' ancestor.(1/6)
7 * A Class must be instantiated before referencing
actual Objects.(1/7)
8 * Existing object can be referenced by the same class
instances.(1/8)
9 * Object's referential content can be transferred
to the other objects.(1/9)
10 * Cross bounds abstraction - Object of the same
class root can be typecasted or act on behalf of other object.(1/10)
1 * A Class inherits from another class or act
as original class.(1/1)
Object oriented language use Class to indicate it's relationship
and where it's come (originate) from. Class can be abstract or inheritance,
and in Delphi Class begin with "T"
Sample below is an original class.
|
Type
........TOrginalClass = Class
End;
|
Sample below is an inheritance class.
|
Type
........TDescendantClass = Class(TAncestor)
End;
|
Back to
TOP
2 * Descendant Class is always greater than or
equal to its' ancestor.(1/2)
Class which inherit from ancestor class always got the SAME ancestor
gene, or ENHANCED MORE than features than ancestor gene (in human
can be less features of course). It's as clear in plain English
as it is and simply as below. Example , if we take another programmer
class, we can create a better and smarter class (don't forget license
from that programmer too).
Back
to TOP
3 * Object can consist of two elements (Data
and Method) (1/3)
When combine these 2 elements we called "Encapsulation".
Data contains property of that object (in English language
would be adjective)
In Delphi, we call data as "Property"
Method is how thing is done (in English would be action or
verb, or adverb)
In Delphi, we call Method as "Procedure" or "Function"
|
Type
..........TAnyClass = Class(TOriginalClass) ....
..........Data = (Object, Real, Extended)
..........Procedure
..........Function
End;
|
Back
to TOP
4 * Any deviation from ancestor causes polymorphism.(1/4)
Any changes from ancestor even very small is call "polymorphism".
Normally we inherit old Class and modify it to improve it's performance.
For example.
|
Type
..........TAnyClass = Class(TOriginalClass)
....
..........Procedure DoThis;
(polymorphism in original class)
End;
Type
..........TAnyNewClass = Class(TAnyClass)
......... (polymorphism
TAnyNewClass to be different from original class)
..........Procedure DoThis;
End;
|
Therefore our new class can change method (procedure) DoThis
|
Procedure TAnyNewClass.DoThis;
..........Begin
..........Inherited; <from
ancestor>
........................... <then
do this new>
End;
|
Or
|
Procedure TAnyNewClass.DoThis;
..........Begin
..........If <condition>
then <do this new>
..........Else Inherited; <do
the old>
End;
|
Or
|
Procedure TAnyNewClass.DoThis;
..........Begin
...................... <do
this new>
..........Inherited; <follow
the old>
End;
|
Back
to TOP
5 * Method of the descendant class with the same
name will replace its ancestor's Method. (1/5)
If you use same "method name" for both descendant and
ancestor, then method of descendant will replace ancestor method.
Programmer could decide if method will follow ancestor or DO all
new methods.
|
Type
..........TAnyClass = Class(TOriginalClass) ....
..........Procedure DoThis;
End;
|
|
Type
..........TAnyNewClass = Class(TAnyClass) ....
..........Procedure DoThis; <replace
ancestor, top of ancestor>
End;
|
Back
to TOP
6 * Method of the descendant class can override and inherit in
part or in full
from its' ancestor. (1/6)
Descendant behavior can be Override (controlled) add Inherited from
ancestor. In Object Pascal we can add "Override" after
Method in descendent in pull the good genes from ancestor in our
new class.
See Example below.
|
Type
..........TAnyClass = Class(TOriginalClass) ....
..........Procedure DoThis; Virtual;
<Virtual = allows control of activity>
End;
|
OR
|
Type
..........TAnyNewClass = Class(TAnyClass) ....
..........Procedure Dothis; Override;
<allow descendant to override, modify
from ancestor>
End;
|
Back
to TOP
7 * A Class must be instantiated before referencing
actual Objects.(1/7)
Class which is not yet "instantiated" is still a Class
and once it's instantiated then it's an Object or "Class Instance".
In Pascal "Instance" refer to by VAR (cut out the "T")
|
Var
..........AnyClass : TAnyClass;
|
"AnyClass" become VAR (Instance type) which refer to
"TAnyClass", BUT need to be further instantiated. (similar
to Pointer which refer to Memory). When you want to refer to "AnyClass",
you can call it by create.
|
..........AnyClass : = TAnyClass.Create;..
|
Back
to TOP
8 * Existing object can be referenced by the
same class instances.(1/8)
Two (2) Instances can refer to the same Object (Think of 2 people
cheat the mobile phone company by having 2 mobile phones using the
same mobile number for easy understanding).
Sample below
|
Type
.......TNewClass = Class (TOriginalClass)
End;
|
Then
|
Var
.......NewClass, AnotherNewClass
: TNewClass;
|
Above sample is declare 2 INSTANCES which
refer to the same CLASS
|
.......NewClass:= AnotherNewClass
|
Refer to 2 INSTANCES which POINT to the same
OBJECT
|
.......NewClass.ObjectData := AnotherNewClass.ObjectData;
|
Back
to TOP
9 * Object's referential content can be transferred
to the other objects.(1/9)
Details of Object Instances can be transferred to other Object Instance.
It's like making a copier on Xerox machine
|
NewClass . Assign (AnotherNewClass)
NewClass . Data . Assign (AnotherNewClass) . Data
* Transfer from right to left
NewClass . MethodReference (AnotherClass).MethodReference
|
Back
to TOP
10 * Cross bounds abstraction - Object of the
same class root can be typecasted or act on behalf of other object.(1/10)
See below for easy understanding
|
|
TMovieStar
|
|
|
|
|
|
|
|
|
TActionHero
|
|
|
|----------
|
----------|----------
|
----------|
|
|
TMacho
|
|
TSpy
|
|
|
|
|
|
|
|
TRambo
|
|
TJamesBond
|
|
|
|
|
|
|
|
TSylVestor
|
|
TRogerMoore
|
* We can modify TMacho to TRambo it is the object of the same root.
By write
* You can not modify TJamesBond to TMacho
Back
to TOP
|