Thursday, April 22, 2010
Inheritance, Polymorphism, Interface
- In Java, all classes, including the classes that make up the Java API, are subclassed from the Object superclass.
[] SUPER CLASS
- Any class above a specific class in the class hierarchy.
[] SUB CLASS
- Any class below a specific class in the class hierarchy.
[] Benefits of Inheritance in OOP
- 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 method
- 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 superclassby providing a new implementation for that method.
[] Final Methods and Classes
- 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.
[] Interfaces
- 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.
Monday, April 19, 2010
WORKING WITH THE JAVA CLASS
– Revolves around the concept of objects as the basic elements of your programs.
– These objects are characterized by their properties and behaviors.
[] 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.
[] Class and Objects
[] Class
– 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.
[]Class Variables
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
● To create an object or an instance of a class, we use the new operator.
● 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.
Methods
– is a separate piece of code that can be called by a main program or any other method to perform some specific function.
● 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.
[] Method Declaration
- Return the character at the specified index. An index ranges from 0 to lenght()-1. The first character of the sequence is at index 0, the next at index1, and so on, as for array indexing.
[] 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);
[] Pass-by-ValueParameter Passing
– 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.
Wednesday, April 14, 2010
Introduction to Java Programming
b.) DEVELOPMENT ENVIRONMENT
An integrated development environment (IDE) also known as integrated design environment or integrated debugging environment is a software application that provides comprehensive facilities to computer programmers for software development.
designed to maximize programmer productivity by providing tightly-knit components with similar user interfaces. This should mean that the programmer has much less mode switching to do than when using discrete development programs.
c.) APPLICATION ENVIRONMENT
There are two main deployment environments:
of class files for all the
browsers supply
d.) DEPLOYMENT ENVIRONMENT
enables a set of computer software programs and data structures to use a virtual machine model for the execution of other computer programs and scripts. The model used by a JVM accepts a form of computer intermediate language commonly referred to as Java bytecode. This language conceptually represents the instruction set of a stack-oriented, capability architecture.
b.) GARBAGE COLLECTION
relies on the observation that a previously allocated memory location that is no longer referenced by any pointers is no longer accessible to the application and is therefore reclaimable. The role of a garbage collector is to identify and recycle these inaccessible allocated memory chunks. There are two families of garbage collecting techniques -- reference counting and tracing.
Garbage collection thread :
– responsible for freeing any memory that can be freed. This happens automatically during the lifetime of the Java program.
– programmer is freed from the burden of having to deallocate that memory themselves
c.) CODE SECURITY
JRE– runs code compiled for a JVM and performs class loading (throughthe class loader), code verification (through the bytecode verifier)and finally code execution
Class Loader– responsible for loading all classes needed for the Java program– adds security by separating the namespaces for the classes of thelocal file system from those that are imported from network sources– After loading all the classes, the memory layout of the executable isthen determined. This adds protection against unauthorized accessto restricted areas of the code since the memory layout isdetermined during runtime
Bytecode verifier– tests the format of the code fragments and checks the codefragments for illegal code that can violate access rights to objects
The easiest way to write a simple program is with a text editor. So, using the text editor of your choice, create a text file with the following text, and be sure to name the text file
ExampleProgram.java. Java programs are case sensitive, so if you type the code in yourself, pay particular attention to the capitalization. b.) COMPILE
A program has to be converted to a form the Java VM can understand so any computer with a Java VM can interpret and run the program. Compiling a Java program means taking the programmer-readable text in your program file (also called source code) and converting it to bytecodes, which are platform-independent instructions for the Java VM.
c.) RUN
Once your program successfully compiles into Java bytecodes, you can interpret and run applications on any Java VM, or interpret and run applets in any Web browser with a Java VM built in such as Netscape or Internet Explorer. Interpreting and running a Java program means invoking the Java VM byte code interpreter, which converts the Java byte codes to platform-dependent machine codes so your computer can understand and run the program.
[] Java Application
D.) What makes Java an Object Oriented Programming Language
Even though Java has the look and feel of C++, it is a wholly independent language which has been designed to be object-oriented from the ground up. In object-oriented programming (OOP), data is treated as objects to which methods are applied. Java's basic execution unit is the class. Advantages of OOP include: reusability of code, extensibility and dynamic applications.
Topicz that have been discuss in JAVA Programming1
Java Programming Structures
Also known as branch, conditional or decision structures, selections allow the program to execute different operations depending on events or conditions. This entails the use of the switch-case statement and the classic If-Then-Else statement.
With its roots in classic mathematics, the if-then-else is used in Java to quantify simple decisions under certain conditions. If something occurs, do this; else, do something different. The simplest form the selection statement can take is just the if-then statement:
if (testVar){
//Commands to execute
}
[]Sequential Structure
A sequence in Java is basically a list of commands that computer executes in order from beginning to end. Most small daily activities are sequential, such as cooking a meal or brushing teeth. A computer, like a human, finds sequential operations to be the simplest and as such the sequence is the default structure for Java programs
BufferedReader provides a readLine method to read a line of text.
[]Formatting output
[]Scanner class
[]File I/O
main method of the "Hello World!" application.