Thursday, April 22, 2010

inheritance, polymorphism,interface

Inheritance
●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.

●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.
●Superclass–Any class above a specific class in the class hierarchy.
●Subclass–Any class below a specific class in the class hierarchy.

Polymorphism
●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 Classes
●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

An 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.

Wednesday, April 14, 2010

1. topics discussed in java 1. provide brief description per topic

Arrays
An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You've seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.

Looping in Java

Loops are another essential part of modern programming languages, with no exception in Java. Their premise is simple: the computer executes some operation or group of operations until told to do otherwise or stop.

The loop comes in several variations, each with their own slight variance in structure and purpose: for loops, while loops, do-while loops, and and the brand new for-each loop. While each has differences, all consist of loosely the same structure:

  • A loop entry, or the point where the loop begins execution
  • A loop test, where the computer tests to see if the loop should begin, repeat, or skip to the next command in the program
  • Iteration, or a single cycle through the loop's group of operations
  • A termination condition, or the specified situation or event that a loop reaches when it's time to stop looping

Java Control Structures

switch (the Java case statement)

//note: shown using char.   //Primitive types char, byte, short, or int  //  can be used in the switch.

switch (charToCheck) { case 'A' : System.out.print('A'); break; //Print 'A' if charToCheck = 'A' case 'B' : System.out.print('B'); break; //Print 'B' if charToCheck = 'B' default : System.out.print('D'); //Print 'D' if charToCheck does // not equal 'A' or 'B' }

//note: if neither break were there, // and if charToCheck = 'A', // the code would print ABD

if else

if (x > 5) {     System.out.println("x is greater than 5"); } else {     System.out.println("x is not greater than 5"); }
Strings
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

METHODS
1.concat()
2.length()
3.IndexOf()
4.LastIndexOf()
5.CharAt()
6.equals()
7.equalsIgnoreCase()
8.StartsWith()
9.endswith()
10.toUpperCase()
11.toLowerCase()
12.replace()
13.Substring()
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.
2.Introduction to java programming
● 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
● 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
● 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.
● The Java technology is:
– A programming language
– A development environment
– An application environment
– A deployment environment
● Java Virtual Machine (JVM)
– an imaginary machine that is implemented by emulating software on a real
machine
– provides the hardware platform specifications to which you compile all Java
technology code
● Bytecode
– a special machine language that can be understood by the Java Virtual
Machine (JVM)
– 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
● 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