The Great Symbian

Anything under the sun goes here!

INHERITANCE
–As the name inheritance suggests an object is able to inherit characteristics from another object. In more concrete terms, an object is able to pass on its state and behaviors to its children. For inheritance to work the objects need to have characteristics in common with each other.

In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass.

● Superclass
– Any class above a specific class in the class hierarchy.

● Subclass
– Any class below a specific class in the class hierarchy.

● Benefits of Inheritance in OOP : Reusability
– Once a behavior (method) is defined in a superclass, that behavior is automatically inherited by all subclasses.

– Thus, you can encode a method only once and they can be used by all subclasses.

– A subclass only needs to implement the differences between itself and the parent.

● Overriding Methods
– If for some reason a derived class needs to have a different implementation of a certain method from that of the superclass, overriding methods could prove to be very useful.

– A subclass can override a method defined in its superclass by providing a new implementation for that method.


●Final Classes
– Classes that cannot be extended
– To declare final classes,

we write,

public final ClassName{
. . .

}
● Final Methods
– Methods that cannot be overridden
– To declare final methods,
we write,

public final [returnType] [methodName]([parameters])
{
. . .
}
– Static methods are automatically final.
POLYMORPHISM
– The ability of a reference variable to change behavior according to what object it is holding.
– This allows multiple objects of different subclasses to be treated as objects of a single superclass, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to.
● Abstract Class
– a class that cannot be instantiated.
– often appears at the top of an object-oriented programming class hierarchy, defining the broad types of actions possible with objects of all subclasses of the class.
INTERFACE
– is a special kind of block containing method signatures (and possibly constants) only.
– defines the signatures of a set of methods, without the body.
– defines a standard and public way of specifying the behavior of classes.
– allows classes, regardless of their locations in the class hierarchy, to implement common behaviors.
– NOTE: interfaces exhibit polymorphism as well, since program may call an interface method, and the proper version of that method will be executed depending on the type of object passed to the interface method call.




· OBJECT-ORIENTED PROGRAMMING

- is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships.

OOP (Object-oriented programming) = encapsulated state + inheritance

Object-Oriented Design
– Focuses on object and classes based on real world scenarios
– Emphasizes state, behavior and interaction of objects
– Advantages:

·Faster development

·Increased quality

·Easier maintenance

·Enhanced modifiability

·Increase software reuse


· ENCAPSULATION
- Combine the data and the operations
- Enclosing of both variables and functions
- Keep details of the data and operations from the users of the ADT
- Once you have created an ADT for complex numbers, say Complex, you can use it in the same way like well-known data types such as integers.
– Principle of hiding design or implementation information not relevant
to the current object


· OBJECT
- An entity with unique identity that encapsulate state
- state can be accessed in a controlled way from outside
- The access is provided by means of methods (procedures that can directly access the internal state)


· CLASS
- A specification of objects in an incremental way
- By inheriting from other classes
- And specifying how its objects (instances) differ from the objects of the inherited classes


·CLASS VARIABLE
– Behave like a global variable
– Can be accessed by all instances of the 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)

Creating objects in Java usually follow these steps:

1- Declaration of a reference variable of the appropriate class which will store a reference to the object.
For example:

//declaring my car
Car myCar;

// declaring my father's car
Car myFatherCar ;

Or combined if they belong to the same appropriate class, separated by a comma:

//declaring my car and my father's car
Car myCar, myFatherCar ;


2- Creating an object
This involves using the new operator and calling a constructor to create an instance of the class.
The new operator returns a reference to a new instance of the Car class.
The reference can be assigned to a reference variable of the appropriate class, here: myCar and myFatherCar.

// instantiating myCar from the class Car,
//having the String "black" as parameter value.
myCar = new Car("black");

// instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
myFatherCar = new Car("blue");


Each object has a unique identity and has its own copy of the fields declared in the class.

The purpose of calling the constructor on the right side of the new operator is to initialize the newly created object.

The declaration and initialization can be combined:

// declaring and instantiating myCar from the class Car,
//having the String "black" as parameter value.
Car myCar = new Car("black");

// declaring and instantiating myFatherCar from the class Car
//having the String "blue" as parameter value.
Car myFatherCar = new Car("blue");


·METHOD
– Describes the behavior of an object
– Also called a function or a procedure
– Student registration system example


· METHOD DECLARATION

To declare methods we write,
(*) {
*
}
– where,
can carry a number of different modifiers
can be any data type (including void)
can be any valid identifier
::= [,]


· STATIC METHOD

public class StudentRecord {
private static int studentCount;
public static int getStudentCount(){
return studentCount;
}
}
– where,
· public- means that the method can be called from objects outside the class
· static-means that the method is static and should be called by typing,
[ClassName].[methodName]. For example, in this case, we call the method
StudentRecord.getStudentCount()
· int- is the return type of the method. This means that the method should return a
value of type int
· getStudentCount- the name of the method
· ()- this means that our method does not have any parameters


· PARAMETER PASSING

- Pass by Value refers to passing a constant or a variable
holding a primitive data type to a method, and

- Pass by Reference refers to passing an object
variable to a method. In both cases a copy of the variable is passed to the method.



A. PRIMITIVE DATA TYPES
- Integral Data Types (byte, short, int long)
- Floating Point (float, double)
- Characters (char, string)
- Boolean (true, false)



B. VARIABLES
A variable is an item of data used to store state of objects.

1. Declaring and Initializing Variables
To declare a variable is as follows,
[=initial value];

2. Outputting Variable Data
In order to output the value of a certain variable, we can use the following commands,
System.out.println()
System.out.print()



C. OPERATORS

1 Arithmetic operators
+, *, /, %, -

2 Increment and Decrement operators
++, --

3 Relational operators

Relational operators compare two values and determines the relationship between those
values. The output of evaluation are the boolean values true or false.
>, >=, <, <=, ==, != 4 Logical operators
Logical operators have one or two boolean operands that yield a boolean result. There
are six logical operators: && (logical AND), & (boolean logical AND), (logical OR),
(boolean logical inclusive OR), ^ (boolean logical exclusive OR), and ! (logical NOT).



D. GETTING INPUT FROM THE KEYBOARD

1. Using buffered reader
·Add this at the top of your code:

import java.io.*;

·Add this statement:

BufferedReader dataIn = new BufferedReader(
new InputStreamReader( System.in) );

· Declare a temporary String variable to get the input, and invoke the readLine()
method to get input from the keyboard. You have to type it inside a try-catch block.

try{
String temp = dataIn.readLine();
}
catch( IOException e ){
System.out.println(“Error in getting input”);
}



2. Using JOptionPane

import javax.swing.JOptionPane;
indicates that we want to import the class JOptionPane from the javax.swing package.
We can also write this as,
import javax.swing.*;
The statement,
name = JOptionPane.showInputDialog("Please enter your name");
creates a JOptionPane input dialog, which will display a dialog with a message, a
textfield and an OK button as shown in the figure. This returns a String which we will
save in the name variable.



E. CONTROL STRUCTURES

1. Decision Control Structures
Decision control structures are Java statements that allows us to select and execute
specific blocks of code while skipping other sections.

· if statement
if( boolean_expression )
statement;

· if-else statement
if( boolean_expression )
statement;
else
statement;

· if-else-if statement
if( boolean_expression1 )
statement1;
else if( boolean_expression2 )
statement2;
else
statement3;

· switch statement
switch( switch_expression ){
case case_selector1:
statement1; //
statement2; //block 1
. . .
//
break;
case case_selector2:
statement1; //
statement2; //block 2
. . .
//
break;
. . .
default:
statement1; //
statement2; //block n
. . .
//
break;
}

2. Repetition Control Structures
Repetition control structures are Java statements that allows us to execute specific
blocks of code a number of times. There are three types of repetition control structures,
the while, do-while and for loops.

·while loop

The while statement has the form,
while( boolean_expression ){
statement1;
statement2;
. . .
}

· do-while loop
The do-while statement has the form,
do{
statement1;
statement2;
. . .
}while( boolean_expression );


· for loop
The for loop has the form,
for (InitializationExpression; LoopCondition; StepExpression){
statement1;
statement2;
. . .
}




F. Java Arrays

1. Declaring Arrays

Arrays must be declared like all variables. When declaring an array, you list the data
type, followed by a set of square brackets[], followed by the identifier name. For
example,
int []ages;
or you can place the brackets after the identifier. For example,
int ages[];
After declaring, we must create the array and specify its length with a constructor
statement. This process in Java is called instantiation (the Java word for creates). In
order to instantiate an object, we need to use a constructor for this. We will cover more
about instantiating objects and constructors later. Take note, that the size of an array
cannot be changed once you've initialized it. For example,
//declaration
int ages[];
//instantiate object
ages = new int[100];
or, can also be written as,
//declare and instantiate
object
int ages[] = new
int[100];

2. Accessing an array element
To access an array element, or a part of the array, you use a number called an index or
a subscript.

3. Array length
In order to get the number of elements in an array, you can use the length field of an
array. The length field of an array returns the size of the array. It can be used by writing,
arrayName.length

4. Multidimensional Arrays
Multidimensional arrays are implemented as arrays of arrays. Multidimensional arrays
are declared by appending the appropriate number of bracket pairs after the array name.



G. JAVA APPLETS

· Life cycle of an applet
-init()
-start()
-stop()
-destroy()

· drawing strings
drawString();
· drawing ovals
drawOval();
· drawing polygons
drawPolygon();
· drawing rectangles
drawRect();
· drawing lines
drawLine();
· changing font color, size, type, and styles






I. FILE HANDLING

· Reading inputs from a text file

· Writing outputs in a text file




A. A LITTLE BIT OF HISTORY

Java was created in 1991 by James Gosling et al. of Sun Microsystems. Initially called
Oak, in honor of the tree outside Gosling's window, its name was changed to Java
because there was already a language called Oak.
The original motivation for Java was the need for platform independent language that
could be embedded in various consumer electronic products like toasters and
refrigerators. One of the first projects developed using Java was a personal hand-held
remote control named Star 7.
At about the same time, the World Wide Web and the Internet were gaining popularity.
Gosling et. al. realized that Java could be used for Internet programming.

B. WHAT IS JAVA TECHNOLOGY?

1. A programming language
As a programming language, Java can create all kinds of applications that you could
create using any conventional programming language.

2. A development environment
As a development environment, Java technology provides you with a large suite of
tools: a compiler, an interpreter, a documentation generator, a class file packaging tool,
and so on.

3. An application environment
Java technology applications are typically general-purpose programs that run on any
machine where the Java runtime environment (JRE) is installed.

4. A deployment environment
There are two main deployment environments: First, the JRE supplied by the Java 2
Software Development Kit (SDK) contains the complete set of class files for all the Java
technology packages, which includes basic language classes, GUI component classes,
and so on. The other main deployment environment is on your web browser. Most
commercial browsers supply a Java technology interpreter and runtime environment.

C. SOME FEATURES OF JAVA

1. The Java Virtual Machine
The Java Virtual Machine is an imaginary machine that is implemented by emulating
software on a real machine. The JVM provides the hardware platform specifications to
which you compile all Java technology code. This specification enables the Java software
to be platform-independent because the compilation is done for a generic machine
known as the JVM.
A bytecode is a special machine language that can be understood by the Java Virtual
Machine (JVM). The bytecode is independent of any particular computer hardware, so
any computer with a Java interpreter can execute the compiled Java program, no matter
what type of computer the program was compiled on.

2. Garbage Collection
Many programming languages allows a programmer to allocate memory during runtime.
However, after using that allocated memory, there should be a way to deallocate that
memory block in order for other programs to use it again. In C, C++ and other
languages the programmer is responsible for this. This can be difficult at times since
there can be instances wherein the programmers forget to deallocate memory and
therefor result to what we call memory leaks.
In Java, the programmer is freed from the burden of having to deallocate that memory
themselves by having what we call the garbage collection thread. The garbage
collection thread is responsible for freeing any memory that can be freed. This happens
automatically during the lifetime of the Java program.

3. Code Security
Code security is attained in Java through the implementation of its Java Runtime
Environment (JRE). The JRE runs code compiled for a JVM and performs class loading
(through the class loader), code verification (through the bytecode verifier) and finally
code execution.
The Class Loader is responsible for loading all classes needed for the Java program. It
adds security by separating the namespaces for the classes of the local file system from
those that are imported from network sources. This limits any Trojan horse applications
since local classes are always loaded first. After loading all the classes, the memory
layout of the executable is then determined. This adds protection against unauthorized
access to restricted areas of the code since the memory layout is determined during
runtime.
After loading the class and layouting of memory, the bytecode verifier then tests the
format of the code fragments and checks the code fragments for illegal code that can
violate access rights to objects.
After all of these have been done, the code is then finally executed.

D. PHASES OF A JAVA PROGRAM
The following figure describes the process of compiling and executing a Java program.

· The first step in creating a Java program is by writing your programs in a text editor.
Examples of text editors you can use are notepad, vi, emacs, etc. This file is stored in a
disk file with the extension .java.

·After creating and saving your Java program, compile the program by using the Java
Compiler.
The output of this process is a file of Java bytecodes with the file extension
.class.

·The .class file is then interpreted by the Java interpreter that converts the bytecodes
into the machine language of the particular computer you are using.

Task
1. Write the program
2. Compile the program
3. Run the program
Tool to use
1. Any text editor
2. Java Compiler
3. Java Interpreter
Output
1. File with .java extension
2. File with .class extension(Java Byte Codes)
3. Program Output
E. DIFFERENCE BETWEEN JAVA APPLICATIONS AND JAVA APPLETS
- Java applets are java programs that run in a web browser, it is dependent on web browsers.
- Java applications are stand alone java programs. It can be run in a console window or a graphical user interface.
F. WHAT MAKES JAVA AN OBJECT ORIENTED LANGUAGE
- Java is considered as an object oriented programming language because it uses "objects" – data structures consisting of datafields and methods together with their interactions – to design applications and computer programs. Java includes features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance.

FEEDS

Add to Google Reader or Homepage