Tuesday, January 25, 2011

Inner classes and its magic

Inner classes have been bugging me for a long time and now I understood what they are.


For each class / interface it can be
top level OR Nested
Each nested class can be
Static OR Inner
Each Inner class can be
Member, Local or Anonymous.


Top level class

Its your normal class. Which has properties, methods, classes etc.
Nested Class

Class which is residing inside another class (or its method)
Static Inner Class

Acts same as Top level class and has package visibility.
Static inner class does not have package level visibility.

public class String{
private static class CaseInsensitiveComparator implements Comparator { ... }

The CaseInsensitiveComparator has only String class visiblity but does not require instantiations.

Member / Inner Class
Does not have package level visibility.
Outer class / enclosing class can not access variables without creating instance of the class.
Inner class can access the private variables of the outer class.
Gives more simplicity and object orientation. i.e. Car as outer, wheels as inner class.
Static fields / methods cannot be part of Inner Classes. Static can be part of only static or top level classes.

i.e.

class top{
int i = 22;

class myNested{
int k=i;
void food() {}

} // end of nested class

void doSomething(){
myNested mn = new MyNested()
mn.food();


}

} // end top

Local class

Example:

public class f extends Applet{

Button apple = new Button();

public void init () {

// Local class starts from here

Class InnerLocalClass implements java.awt.ActionListener {
int i=1;

public void ActionPerformed(Event e){
System.out.println("Somehting has happned ...");
} // end method

}// end inner local class

// Local class ends here

}// end init method

add(apple);
apple.addActionListener(new InnerLocalClass);

}// end class


Anonymous Classes

We saw that a method that has inner class and that is accessible by the enclosing class. Instance of Local Class is passes to action listener to track events. Now what if we can add the methods / implementation directly to any action listener.

i.e.

public class f extends Applet{

Button apple = new Button("Text Name");

public void init(){
int clickCount = 0;
add(apple);
apple.addActionListener(new ActionListener(){
public void ActionPerformed(ActionEvent e) { ... do something ... }
// you can add more listeners here
}
}
}

clickCount cannot be part of do something. Because it is not final. So when the code is compiled the clickCount is not passes to Inner Anonymous class unless it has final as access modifier.

How these classes are compiled altogether:
- Compiler creates outerclass$something for inner classes and created when compiled. For static they are treated as top level classes.

Sunday, January 23, 2011

Lessons learned from computer science interviews

Interviews and its questions. They always look easy after getting out of interview session. Most of the time people tend to ask technical questions and specifically programming ones. In this blog post I am writing my experience and advises while writing a program.

In the program to understand we have logical parts such as input, output and logical main. To start with your program

:> Identify the returned stuff. Say int is returned to find max out of an array
:> Put a logical method name with proper input or receiving parameters
:> In the start of the program initialize some variables if required
- counter, start and end pointer to string, length of the string
:> if recursive program then check on null or exit conditions in start of program.
:> in recursive program the return can be associative with recursion.
- i.e. return ( node.value < node.right.value && checkTreeisBST(node.right) )
:> do a dry run after finishing the program and test it for edge conditions

A good guide to programming questions is Crack the Interview . Here the answers are wrong but questions are right. So don't trust the answer.

Wednesday, January 12, 2011

XML Parsing Techniques

Parsing is a process that involves break the xml text into nodes.

Pull Parsing:

Application has to pull the next node or element from the parser. Application asks the parse to next node. Pull parsing does not do varification (checking the xml against schema) however definitely do verification.

Push Parsing:

Parser sends the notification to the application about the type of XML nodes / pieces found during parsing. It is also knows as Event Based Parsing. SAX (Simple API for XML) is fast parsing mechnism, but application has to match the speed of the SAX parser for each event. Same as above about validation and verification.

One Step Parsing:

Parser generates the tree based upon the XML document structure. DOM (Docuemnt Object Model) is one of the parsing standard. Very useful when traversing is required in the tree.

Hybrid Parsing:

The application thinks its working with one step parser that has processed the whole document while parsing process has just started. As the pplication keeps accessing more objects on the DOM tree the parsing continues incrementally.

XML Parsing Models and Their Trade-Offs:


ModelControl of ParsingControl of ContextMemory EfficiencyComputational EfficinecySimplicity
PullApplicationApplicationHigh Highest Low
Push (SAX) Parser Application High High Low
One-step(DOM) Parser Parser Lowest Lowest High
One-step (JDOM) Parser Parser Low Low Highest
Hybrid (DOM) Parser Parser Medium Medium High
Hybrid (JDOM) Parser Parser Medium Medium Highest


References:
http://media.techtarget.com/searchSAP/downloads/Building_Web_Services_with_Java_CH02.pdf