Wednesday, December 7, 2011

Association, Aggregation & Composition

Association : An association is  the relation between two objects. In other words its connectivity between two objects. Aggregation and compositions are form of association. 
Aggregation: This is a part of a whole relationship where a part can exist without a whole.
Aggregation is a weak association.  Also called “has a” relationship.
Composition : This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted.
Composition is a Strong association.
Example:
A House contains Room and TV. Relationship between House and TV is aggregation and relation between House and Room is composition.
A TV can exist without a House-> Weak association (Aggregation)
A Room cannot exist without a House -> Strong association (Composition).

Java Example :
Aggregation

Class LPG{
}

Class Car{
Car(LPG objLPG) //Object is passed from outside   - weak relationship –LPG can exist without a car
{
}
}

Composition
Class Engine{}
Class Car{
                Car(){
                Engine objEng = new Engine();  //lives within car object   - Strong relationship: Engine cannot exist without car.
}
}








Tuesday, December 6, 2011

Class File (binary file)

We all(Java Developers) know, in the java programming language java source files (.java) are compiled into machine readable (.class) files and the .class files have byte code.


In this section  we will look into the class file structure (class file format) .

The ClassFile Structure (As per The Java Virtual Machine)

ClassFile {
                 u4 magic;
                 u2 minor_version;
                 u2 major_version;
                 u2 constant_pool_count;
                 cp_info constant_pool[constant_pool_count-1];
                 u2 access_flags;
                 u2 this_class;
                 u2 super_class;
                 u2 interfaces_count;
                 u2 interfaces[interfaces_count];
                 u2 fields_count;
                 field_info fields[fields_count];
                 u2 methods_count;
                 method_info methods[methods_count];
                 u2 attributes_count;
                 attribute_info attributes[attributes_count];
}
Magic Number: Identifying the class file format; it has the value 0xCAFEBABE.
Version of Class file :  The values of the minor_version and major_version items are the minor and major version numbers of this class file.
Constant pool : The constant pool table is where most of the literal constant values are stored. This includes values such as numbers of all sorts, strings, identifier names, references to classes and methods, and type descriptors. 
Access Flag : denote access permissions to class or interface.
This class : The value of the this_class item must be a valid index into the constant_pool. The constant pool entry at that index must be a Constant_claa_info structure representing the class or interface defined by this class file.
Super Class : Identify the superclass of this class.
Interfaces : Identify any interfaces in the class. (interfaces_count, interfaces)
Fields : Fields in the class.( fields_count, field_info fields)
Methods : Methods in the class (method_count, method_info)
Attributes : Attributes in the class  (attributes_count, attribute_info)


Simple example with binary code

Compiled with J2SE 6 and opened the class file with Binary viewer (Hexadecimal).
Some of the file Structure sections with respect to above example
Magic number : CAFEBABE
Minor version: 0000
Major Version : J2SE 6.0 = 50 (0x32 hex) – 0032
Access flag : 0001– public access




Sunday, December 4, 2011

Eclipse Shortcuts

CTRL + D
Delete row. No need to grab the mouse and select the line. simple press ctrl + d to delete the row.
ALT + SHIFT + N
Open the dailog box, which allow to create new package/class/Project ... etc.


ALT + UP/Down arrow
Very useful at the time of rearranging the code. move up/down the selected row(code).

ALT + Left/Right arrow
Move the last location you edited. Imagine you just created a class Test1, and now you are working on a class Test2. Now, if you need to look at the Test1 class, just press Alt+Left arrow. Alt + Right arrow brings you back to Test2.


CTRL + SHIFT + R
This shortcut opens a dialog box that accepts the name of the file you're looking for. It accepts wildcard characters. Typing *.java will give you the list of all files that ends with .java.

CTRL + SHIFT + T
This will give the list of files(a class, an interface) which are present in packages ( don't have the source files in workspace, only have the class files ).


CTRL + O
which gives you a list of methods in file that match what you've typed


CTRL + E
Go to the open editors.

CTRL + T
Get the dialog box, which toggles between supertypes and subtypes.

CTRL + L : Go to Line


CTRL + SHIFT + O
It resolves the import issues

F3 : Jump to definition of methods

CTRL + 3 (Most useful shortcut )
Quick Access (Ctrl + 3) is your window to anything. perspectives, views, open editors, commands, menus ... just press ctrl + 3 and start typing, and it will show you the possible matches.