Monday, April 19, 2010

Working w/ the Java Class

Object Oriented Programming - is a programming paradigm that uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s.

Encapsulation - the method of hiding certain elements of the implementation of a certain class.
– By placing a boundary around the properties and methods of our objects, we can prevent our programs from having side effects wherein programs have their variables changed in unexpected ways.

Classes and Objects - can be thought of as a template, a prototype or a blueprint of an object
– is the fundamental structure in object-oriented programming

• Two types of class members:
– Fields (properties or attributes)
• specify the data types defined by the class
– Methods.
• specify the operations

Object - is composed of a set of data (properties) which are variables describing the essential characteristics of the object, and consists of a set of methods (behavior) that describes how an object behaves.
– An object is an instance of a class.

Classes provide the benefit of reusability.
• Software programmers can use a class over and over again
to create many objects.

Class Variable - a class variable is a variable defined in a class (i.e. a member variable) of which a single copy exists, regardless of how many objects of the class exist.

Classes consist of
– Instance variables
– Instance methods
– Class Variables (static member variables)
•variables that belong to the whole class.
•This means that they have the same value for all the objects in the same class.

•Class Instantiation - the process of creating objects from a class is called instantiation. So an object is always an instance of a class which represents the blueprint.
The object is constructed using the class and must be created before being used in a program.
Objects are manipulated through object references (also called reference values or simply references)

•The new operator
– allocates a memory for that object and returns a reference of that
memory location to you.
– When you create an object, you actually invoke the class'
constructor.
•The constructor
– is a method where you place all the initializations, it has the same
name as the class.

Method - A Java method is a set of Java statements which can be included inside a Java class.

Java methods are similar to functions or procedures in other programming languages.

•The following are characteristics of methods:
– It can return one or no values
– It may accept as many parameters it needs or no parameter at all.
Parameters are also called arguments.
– After the method has finished execution, it goes back to the method
that called it.

Every Java program must have one main() method.

Here is the main() method from a Java program which prints "Hello World":

public static void main (String[] args) {
// This Java program prints "Hello World!"
System.out.println{"Hello World!");
}

Method Declaration - a method declaration is the heading of a method containing the name of the method, its parameters, and its access level. The method heading in Java is organized as such:

[access keywords] [return type] [method name] ( [parameters separated by commas] )

for instance:

public String toString(); is public (accessible by any class), returns a String, is called toString, and takes no parameters.

Other features could be added to the method declaration for a more specialized method such as static (method could be called without an object of that class), native (implemented using the native code, usually what C has already done, i.e. square root, power etc.).

Static Methods - methods that can be invoked without instantiating a class (means without invoking the new keyword).
– Static methods belong to the class as a whole and not to a certain instance (or object) of a class.
– Static methods are distinguished from instance methods in a class definition by the keyword static.
• To call a static method, just type,
Classname.staticMethodName(params);

●Parameter Passing
Pass-by-Value
when a pass-by-value occurs, the method makes a copy of the value of the variable passed to the method. The method cannot accidentally modify the original argument even if it modifies the

parameters during calculations.
– all primitive data types when passed to a method are pass-by-value.

Pass-by-Reference – When a pass-by-reference occurs, the reference to an object is passed to the calling method. This means that, the method makes a copy of the reference of the variable passed to the method.
– However, unlike in pass-by-value, the method can modify the actual object that the reference is pointing to, since, although different references are used in the methods, the location of the data they are pointing to is the same.

No comments:

Post a Comment