Friday, October 28, 2011

SUPER();

call to super(); in the child class constructor must be the first line .

Monday, July 4, 2011

Java

1. Variables

As you learned in the previous lesson, an object stores its state in fields.
int cadence = 0;
int speed = 0;
int gear = 1;

The What Is an Object? discussion introduced you to fields, but you probably have still a few questions, such as: What are the rules and conventions for naming a field? Besides int, what other data types are there? Do fields have to be initialized when they are declared? Are fields assigned a default value if they are not explicitly initialized? We'll explore the answers to such questions in this lesson, but before we do, there are a few technical distinctions you must first become aware of. In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.

The Java programming language defines the following kinds of variables:

  • Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in "non-static fields", that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.

  • Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.

  • Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

  • Parameters You've already seen examples of parameters, both in the Bicycle class and in the main method of the "Hello World!" application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you'll learn about later in the tutorial.
Having said that, the remainder of this tutorial uses the following general guidelines when discussing fields and variables. If we are talking about "fields in general" (excluding local variables and parameters), we may simply say "fields". If the discussion applies to "all of the above", we may simply say "variables". If the context calls for a distinction, we will use specific terms (static field, local variables, etc.) as appropriate. You may also occasionally see the term "member" used as well. A type's fields, methods, and nested types are collectively called its members.
http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html

2. Initializing Fields

As you have seen, you can often provide an initial value for a field in its declaration:
public class BedAndBreakfast {      public static int capacity = 10;  //initialize to 10      private boolean full = false;  //initialize to false } 
This works well when the initialization value is available and the initialization can be put on one line. However, this form of initialization has limitations because of its simplicity. If initialization requires some logic (for example, error handling or a for loop to fill a complex array), simple assignment is inadequate. Instance variables can be initialized in constructors, where error handling or other logic can be used. To provide the same capability for class variables, the Java programming language includes static initialization blocks.

Note: It is not necessary to declare fields at the beginning of the class definition, although this is the most common practice. It is only necessary that they be declared and initialized before they are used.

Static Initialization Blocks

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example:
static {      // whatever code is needed for initialization goes here } 
A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

There is an alternative to static blocks — you can write a private static method:

class Whatever {     public static varType myVar = initializeClassVariable();       private static varType initializeClassVariable() {          //initialization code goes here     } } 
The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.

Initializing Instance Members

Normally, you would put code to initialize an instance variable in a constructor. There are two alternatives to using a constructor to initialize instance variables: initializer blocks and final methods.

Initializer blocks for instance variables look just like static initializer blocks, but without the static keyword:

{      // whatever code is needed for initialization goes here } 
The Java compiler copies initializer blocks into every constructor. Therefore, this approach can be used to share a block of code between multiple constructors.

A final method cannot be overridden in a subclass. This is discussed in the lesson on interfaces and inheritance. Here is an example of using a final method for initializing an instance variable:

class Whatever {     private varType myVar = initializeInstanceVariable();       protected final varType initializeInstanceVariable() {          //initialization code goes here     } }  
This is especially useful if subclasses might want to reuse the initialization method. The method is final because calling non-final methods during instance initialization can cause problems. Joshua Bloch describes this in more detail in Effective Java.

http://download.oracle.com/javase/tutorial/java/javaOO/initial.html

3. final keyword

http://renaud.waldura.com/doc/java/final-keyword.shtml#vars

-------------------------------------------------------------------------------------------------------------------------
Over riding and over loading in Java

Method Signature
Name of the method + parameters(no.of parameters, types of parameters)


ex: For method int java_riding(int a, float c)
The method signature is
java_riding(int a, float c)


METHOD OVERLOADING


Method signature must be different.

METHOD OVERRIDING

An instance method in the subclass said to be overriding a method in super class if it has same signature and return type of the method ,that is present in super class .

* Overriding method can return a subtype of the return type of the overridden method.

METHOD HIDING

Same as Method Overriding, but both the overridden and overriding methods must be static or class methods.


EXCEPTION AND ERRORS

java.lang.Throwable .Both error and excpetion are subclasses of Throwable.

EXCEPTION :
It is a reasonable condition that can occur while running program which we might wish to catch.

ERROR : It is an abnormal condition that can occur while running program which we should not try to catch. For Example: The ThreadDeath Error

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.






Friday, June 3, 2011

C .. My Love

Links

Dennis Ritchie website .. where we can find many links about C.
http://cm.bell-labs.com/cm/cs/who/dmr/index.html

C
http://cm.bell-labs.com/cm/cs/who/dmr/ctut.pdf
The below one explains the lvalue
http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf

http://cm.bell-labs.com/cm/cs/who/dmr/mdmpipe.html

Inter Process communication
http://cm.bell-labs.com/cm/cs/who/dmr/ipcpaper.html

Thursday, June 2, 2011

Programming with a perspective of how things work

http://csapp.cs.cmu.edu/public/instructors.html

CALL STACK

This is about how a program works. The one below says how a call stack functions.
http://en.wikipedia.org/wiki/Call_stack
http://en.wikipedia.org/wiki/Calling_convention

The one below explains the call sequence or call stack or stack or execution stack mechanism in C
http://cm.bell-labs.com/cm/cs/who/dmr/clcs.html