Thursday, March 6, 2014

Classes 
 A class is constructed of member which can be variables or methods.
•    Variables are used to store the data for the class. They are specific to
an object; every object instantiated from the class declaration has its
own copy of the variable. Such variables are known as instance
variables.
• Methods are the functions that operate on the data and define the
object's behavior. They are often declared to operate on the instance
variables of the class, and are known as instance methods.
A class is not an object. A class is an outline that defines how an object behaves
when the object is created from the class. Obtain concrete objects by instantiating
a previously defined class. Many objects can be instantiated from one class
definition.


Declaration of Classes
A class contains the properties, methods, and variables needed by the object.
Create a class by right-clicking the Classes node of the AOT and selecting New
Class, or by using the Class Wizard at Tools > Development Tools > Wizards.
A class always contains a classDeclaration node and two methods:

ClassDeclaration: Specifies the name of the class and necessary
variables. It can also specify inheritance.

New(): This method instantiates a new object. You can also assign
values to object variables using this method.
Finalize(): This method terminates the object. It
These methods enable the object instantiation to occur. An infinite number of
methods can be added to a class.

Method Access Control
Methods in a class are always available to other methods in the class. However
they should not always be available to methods in other classes. To control
access to a method, a methods modifier is placed in the method definition.
Modifiers
There are three modifiers available.
Public allows the method to be called from any code in the
application.
Protected allows the method to be called only by methods in the
same class or subclasses of the class in which the method is defined.
Private allows the method to be called only by methods in the same
class in which the method is defined.
When a new method is created, the default modifier of the method is Private.

Inheritance :

Inheritance is a concept where one class can inherit all the methods and variables
from another class. A child class inherits the methods of the parent class.
Additional methods can be applied to the child class, and inherited methods can
be overridden. This means the child class is not always identical to the parent
class.
The advantage of inheritance in object-oriented programming is that code can be
written one time and be reused many times.

Extend a Class
To have a class extend another class, modify the classDeclaration code of the
child class by adding extends and then the parent class

Objects created from the Child
• Have at least the same methods and variables as the Parent.
• Can have methods and variables that do not exist in the Parent.
• Can have methods from the Parent that are overridden or altered, in the Child.

For example, if the Parent contains four methods:
Method1, Method2, Method3, Method4
the Child can have two of those methods overridden, plus an additional method.
Method1, Method3, Method5
If the code refers to one of the methods in the Child that has not been overridden,
the system will go to the Parent to retrieve the method. If that method then calls a
method that is in the Child, it will revert back to the Child.

Objects
Objects are created at run-time using the classes defined in the AOT. To create
an object from a class, the class has to be instantiated.
Instantiating a Class
Object instance methods can only be used when an object is instantiated from a
specific class. To instantiate a class, is to create a new instance of it. You can
have multiple instances of a class, meaning, you can have the same code running
multiple times. Think of how in Microsoft® Word®, you can have two documents
open at the same time. They are both running the same program, but are two
separate instances of that program.
When you create a new object, call the new() method to execute the code when
the object is instantiated.

Once the class is instantiated, to execute a specific method from your code, use
the reference variable followed by a "dot" operator then the method name.
myClass myClass;
;
myClass = new myClass();
myClass.myMethod();
The objective of following lab is to instantiate a class, and then execute a method
from that class.

No comments:

Post a Comment