Showing posts with label encapsulation_classes. Show all posts
Showing posts with label encapsulation_classes. Show all posts

Friday, 22 April 2011

Object Creation and Destruction

To create an object of a particular class use the new operator. For example, now that there is a constructor for the Box class you can make specific instances or discrete copies of a box by using the assignment operator and the new memory allocation operator as in:
Box box_1=new Box(3,4,5);
Note: Once a class has been specified, a datatype exists with the same name.
You do not need to destroy or remove an object when it is no longer needed. Java automatically flags unused objects and applies garbage collection when appropriate. However you may occasionally need to use the finalize() method to insure that a non-Java resource such as a file handle or a window font character is released first. The general form is:
void finalize()
{
//cleanup code goes here
super.finalize() //parent too!
}

OOP in java

Class Syntax in Java

A class is a template or prototype for each of many object instances made to the class design. The class specifies the properties (data) and methods (actions) that objects can work with.

Syntax of Class

The syntax for a class is:
["public"] ["abstract"|"final"]"class" Class_name
["extends" object_name] ["implements" interface_name]
"{"
// properties declarations
// behavior declarations
"}"
The first optional group indicates the visibility or scope of accessibility from other objects. public means visible everywhere. The default (ie. omitted) is package (aka friendly) or visible within the current package only.
The second optional group indicates the capability of a class to be inherited or extended by other classes. abstract classes must be extended and final classes can never be extended by inheritance. The default (ie. omitted) indicates that the class may or may not be extended at the programmers discretion.
Class_name has initial letter capitalized by Java convention.
The third option of extends is described in the tutorial on inheritance.
The fourth option of implements is described in the tutorial on interfaces.
A simple example of a class specification is a box. The box has length, width and height properties as well as methods for setting dimensions and displaying its volume.
public class Box
{
// what are the properties or fields
private int length, width, height;

// what are the actions or methods
public void setLength(int p) {length=p;}
public void setWidth(int p) {width=p;}
public void setHeight(int p) {height=p;}
public void showVolume() {System.out.println(length*width*height);}
}
Note 1: There is no main method in a class defining template!
Note 2: Class names begin with a capital. Use lowercase for all other names.
Note 3: It is good programming practice to write separate files for the class templates and the driver or main user program. This allows separate compilation as well as class reuse by other driver programs. A class file can contain more than one associated class but normally its filename is that of the first defined file. A driver program is named the same as the class that contains the main(). Its file may contain other classes as well.

Field Values or Properties
Methods
Types of methods


Encapsulation

Encapsulation is the ability of an object to be a container (or capsule) for related properties (ie. data variables) and methods (ie. functions). Older languages did not enforce any property/method relationships. This often resulted in side effects where variables had their contents changed or reused in unexpected ways and spaghetti code that was difficult to unravel, understand and maintain. Encapsulation is one of three fundamental principles in object oriented programming.

Data hiding is the ability of objects to shield variables from external access. It is a useful consequence of the encapsulation principle. Those variables marked as private can only be seen or modified through the use of public accessor and mutator methods. This permits validity checking at run time. Access to other variables can be allowed but with tight control on how it is done. Methods can also be completely hidden from external use. Those that are made visible externally can only be called by using the object's front door (ie. there is no 'goto' branching concept).
A class is a template or prototype for each of many object instances made to the class design. The class specifies the properties (data) and methods (actions) that objects can work with.

Thursday, 21 April 2011

Types of Methods in java

Constructor methods allow class objects to be created with fields initialized to values as determined by the methods' parameters. This allows objects to start with values appropriate to use (eg. salary set to a base level or employeeNumber set to an incrementing value to guarantee uniqueness). For our simple box class:
public Box() {length=0;width=0;height=0;} // default is point
public Box(int l,int w,int h) // allows giving initial size
{length=l; width=w; height=h;}
Note that there is no class keyword or return datatype keyword. Also the method name is the same as the class name. This is what marks the fragment as a constructor method. If no constructor method is defined for a class, a default constructor is automatically used to initialize all fields to 0, false or unicode(0) as appropriate to the datatype.
One clever programming device is to declare the constructor with no parameters as private and use it to initialize all properties. Then other constructors can first call it using this() and then do their own specific property validations/initialization.
Accessor (or observer) methods read property (ie. field variable) values and are conventionally named getFoobar() or whatever the property is called.
Mutator (or transformer) methods set property values and are often named setFoobar() etc. Mutators can be used to ensure that the property's value is valid in both range and type.
It is good programming practice to make each property in a class private and include accessor and mutator methods for them. This is an example of object encapsulization. The exceptions to writing accessor/mutator methods for each property is for those that are used only within the class itself or for properties that are set in more complex ways.
Helper methods are those routines that are useful within the class methods but not outside the class. They can help in code modularization. Normally they are assigned private access to restrict use.
Recursive methods are methods that are defined in terms of themselves. A classic recursion is factorials where n factorial is the product of positive integer n and all the products before it down to one. In Java this could be programmed as:
class Factorial
{
int factorial(int n)
{
if (n==1){return 1};
return (n*factorial(n-1));
}
}
Caution: This short method is not very well written as negative and floating calling parameters are illegal in factorials and will cause problems in terminating the loop. Bad input should always be trapped.

Saturday, 12 December 2009

Functions or Methods in java

Class behaviour are represented in Java by methods. To declare a method use the following syntax:
[ "public" | "private" | "protected" ] [ "final" ]
[ "static" | "abstract" | "native" ]
return_data_type method_name "(" parameter_list ")"
"{"
// some defining actions
"}"
Accessibility keywords are the same as for properties. The default (ie. omitted) is package (aka friendly) or visible within the current package only.
static methods are shared by all members and exist for all runtime. Static methods can be referenced without creating an instance of the class. abstract methods must be redefined on inheritance. native methods are written in C but accessible from Java.
The return_data_type defines the type of value that the calling routine receives from the object (the reply message in object terminology). It can be any of the primitive types or the reserved word void (default value) if no message is to be returned. The statement return varName; is used to declare the value to be returned to the calling routine.
The parameter_list can contain from zero to many entries of datatype varName pairs. Entries are separated by commas. Parameters are passed by value, thus upholding the encapsulation principle by not allowing unexpected changes or side effects. Object references (such as arrays) can also be passed.
Some examples of method header parameter lists are:
 
public static void example1() {}
public static int add2(int x) {x+=2; return x;}
public static double example3(int x, double d) {return x*d;}
public static void example4(int x, int y, boolean flag) {}
public static void example5(int arr[]) {} // note: this is object

Note: Differences from cpp 
Types of methods
Pass by value or reference