Wednesday, June 21, 2006

Introduction

This is my first effort to prepare notes about the basic java for Dummies. I may not claim that it covers everything but I try to cover the some of the important areas that beginners need to know. Please note that this is not a guide for java but it may give you an overall view. You may need to visit http://www.java.sun.com/ to get the language specification and documentation. It may be useful for those who have experience in client server technologies and willing to switch over to J2EE world.



1. Object Oriented Programming Principles

Before you start studying Java you may need to know the principles of the OOPs.



  • The Open/Closed Principle: Software entities (classes, modules, etc) should be open for extension, but closed for modification.

  • The Liskov Substitution Principle: Derived classes must be usable through the base class interface without the need for the user to know the difference.
  • The Dependency Inversion Principle: Details should depend upon abstractions. Abstractions should not depend upon details.

  • The Interface Segregation Principle: Many client specific interfaces are better than one general purpose interface/

  • The Reuse/Release Equivalency Principle: The granule of reuse is the same as the granule of release. Only components that are released through a tracking system can be effectively reused.

  • The Common Closure Principle: Classes that change together, belong together.

  • The Common Reuse Principle: CClasses that aren't reused together should not be grouped together.

  • The Acyclic Dependencies Principle: The dependency structure for released components must be a directed acyclic graph. There can be no cycles.

  • The Stable Dependencies Principle: Dependencies between released categories must run in the direction of stability. The dependee must be more stable than the depender.

  • The Stable Abstractions Principle: The more stable a class category is, the more it must consist of abstract classes. A completely stable category should consist of nothing but abstract classes.

2. OOPs Concepts

2.1. Object

Object is an instance of a class. The instance is an executable copy of a class is called Object. It maintains the state in its variable. The behavior is implemented in the method.

Example,
A bicycle is an object.

2.2. Class

Class is a structure that defines the data and method that work on that data.

Example,
Bicycle is a family of bicycles; it has state (gear and two wheels) and behavior (change gear and break). The bicycle state and behavior is different from other bicycles.

2.3. Encapsulation

The object (instance) state and behavior are hidden from the other classes that use it. To achieve the good encapsulation, all the variables have to be declared private and accessed via (assessors and mutators).

Example;
public class TestClass {
/* declare name */
private String name; /* The object state is protected from outsiders */
public class() {
this.name=null;
}

/**
Sets the name
@param String name
*/
public void setName(String name) { /* Set the state through the set method */
this.name=name;
}

/**
Gets the name
@return String name
**/
public String getName() { /*Get the state through the get method */
return this.name;
}
}

2.4. Inheritance

A class inherits state and behavior from the base class. It helps to reuse the code and code organization

2.5. Polymorphism

It is used to redefine the method behavior especially in the derived class.

2.6. Composition, Aggregation, Association and Generalization

Composition is a “is a” relationship. It is implemented by using the inheritance.
Example,
Public class Employee extends Person { /** it is a “is a relationship **/

}


2.7. Aggregation is a “has a” relationship. It is implemented using containment.
Example,
public class Car {
Engine engine; /** It is a “has a” relationship. **/
Vector wheel;
Vector seats;
}

2.8 Association

Class A has a path to Class B as an attribute or in a method call that returns the class B. Class A has an association with the class B.

2.9 Generalization

Generalization is a super class-subclass relationship.

InputStream is a Generalization of FilterInputStream, which is a generalization of DataInputStream.

3. Opps in Java

3.1 Inheritance

A class will be automatically containing the variable and methods defined in their super types and all of its ancestors.

Java does not support multiple class inheritance.

Subclass inherits the following,
· The public or protected variables
· Access the variable with no access specifier as long as the subclass is in the same package.
· Subclass does not inherit the super class member if the subclass declares the member with the same name.

Override
Providing different implementation in the subclass of the class that originally defined the method.

Overriding Member Variables(Hiding)
public class Super {
Number aNumber;
}

public class Base extends {
Float aNumber; // The member variable aNumber hides the Super.aNumber. But the base variable can be accessed by super.aNumber.
}

Overriding Methods

Thumb rules when overriding the methods,

· The return type, method name, number and type of arguments must match those in the overridden method.
· The overriding method can have different throws clause as long as the overridden method does not declare any throws clause.
· The access specifier for the overriding method can allow more access, but not less. Example, protected method in the super class can be made public not private.

A subclass cannot override in the following scenario,
· If the super class method is final and static.

3.2 Polymorphism
It allows derived class to redefine the methods. It is achieved through the overloading.

The method overloading can be achieved by providing different no.of arguments or data types with same return type and method name.


3.3 Abstract Class

An abstract class is a class that can only be subclassed-- it cannot be instantiated.
abstract classname {
}

The abstract class can define the methods without any implementation. So the base class will implement the method.

3.4 Final Class and Methods

A class can be declared as a final to provide the security, that the class cannot be inherited or subclassed.

The methods also can be protected from being overridden.
Example,
public final Test {
public final void printX() {
System.out.println(“X”);
}
}


3.5 Interface

An interface is a named collection of method definitions (without implementations). It can also declare the constants.

The following is the syntax of the Interface.

public interface interfacename
Extends SuperInterface /** Can extend any number of Interfaces. **/
{
Declare constants.
/** public static final variable name **/
Define methods.
/** public abstrace returntype methodname {arguments} throws clause if any; **/
}

The abstract modifier is implicitly used for interface and method declarations within the interface declaration. It should be avoided for interface and method declarations.

The following are the difference between the interface and the abstract class.

4. Class

Class Declaration

public // the class publicly accessible
abstract // instance can not be created
final // class can not be sub classed
class class name
extends
implements
{
//constructors
//member variables
//methods
//finalize block
}

Constructor

The constructor is used to initialize new object of that type. The constructor has the same name as the class.
· Java supports the name overloading for the constructor so that the constructor can have any number of constructors with the same name.
· Default constructor is provided if the constructor is not defined for the class.
· The following are the scope of the constructor,


1. private
No other class can create the instance. It is used in the singleton pattern.
2. protected
Only the subclasses of the class and classes in the same package can create the instance.
3. public
Any class can create the instance.
4. no specifier
Only classes with in the same package can create the instance.

Member Variable

The following is the syntax for the member variable declaration,

Access level
Specifies the access level for other classes,

Specifier Class Subclass Package World

private Class
protected Class Subclass Package
public Class Subclass Package World
package Class Package

if left unspecified





private
It is more restricted access level. The private variables and methods can be used only with in the class.

protected
The member variables and methods can be accessed only with in the family (class, subclass and package).

Example,
package family;
public class SuperClass {
protected int x=0;
protected void printWord(String word){
System.out.println(word);
}
}

package family;
public class BaseClass extends SuperClass {
public void accessMethod(SuperClass s) {
s.x; //legal
s.printlWord(“Hello World”); //legal
}
}

The above code shows that the protected variable and method access is legal with in the same package of the super class.

package outsiders;
import family.SuperClass;
public class Outsider extends SuperClass {
public void accessMethod(SuperClass s) {
s.x; //illegal
s.printWord(“Hello World”); //illegal
}
}
The above code shows that the protected variable and method access is illegal with in the same package of the super class.

public
The member variables and methods are visible to the outsiders.
package
The package scope is used if none of the above specifiers are used.
static
It is used to declare the class variable instance of instance variable.
final
It makes sure that the variable value cannot be changed.
transient

volatile

type

name
The name can be any java identifier and start with lower case letter.

Implementing Methods

The following is the method syntax,

{access specifier} {return type} {method name} {arguments} {throws clause}

access specifier
The access specifier can be public, protected, private and no specifier.
static
It declares the method as a class method instead of instance method.
final
The final methods cannot be overridden.
abstract
An abstract method has no implementation and must be a member of abstract class.
native

synchronized
It makes sure that the method is thread safe.