Monday, November 1, 2010

Singleton or Abstract Constructor Pattern

Singleton Class !!! As the name suggests there some thing single related to these type of classes. Single object ... just one object can be created and in order to achieve that we need to code appropriately. Lets see a sample code or rather an attempt to create class singleton. Some new concepts (even by java compiler and run time environment) are illustrated.


public class Employee {


Employee e = null;

Employee(){

if(e == null)
e = new Employee();

return e;

}
}



  1. We know constructor don't have any return type but it can still return object of the class itself. Interesting may be there is Abstract Constructor Pattern and this is the basis. Some one will discover soon.
  2. Interviewee confirmed thrice with confidence that this code not only compiles but at run time it doesn't end up in recursion calling constructor of class again from constructor again with a condition that is not on static variable.



Friday, July 30, 2010

Crash course

After break of few weeks, I was called to be interviewer. The interviewee didn't disappoint, he provided several concepts, too many to handle (remember) at a time. Here are some of them. Enjoy !!!!

1. Controller is part of business layer.
2. JDK 1.5 (latest version of java :o) is re-branded version of JDK 1.4.
3. How to make a class singleton ?

Ans by interviewee: Create all constructors private.

"Hmm, seems he may know it"

That's it. class becomes singelton. "This is half hearted attempt to make class singleton, next we tried to help, propably this fellow knows"

class Tyre{

private Tyre(){
..
}

public static void main(String args[]){

new Tyre();
new Tyre();

}

Q: Will this class compile?
A: Yes

after some pause, don't ask why there was a pause.

Q: Ooooookay, how many objects of Tyre would be created ?
A: One. because it's singleton class.:o

4. Struts 1.2 is protocol dependent and struts 2 is protocol independent.That is difference between two versions.
5. To upload a file on FTP server we can create a servlet on server side and implement its doPost method. "FTP has post method :o may be in new re-branded version of FTP"
6. TCP / IP & OSI models are layered architecture. Since netorking is involved in servlets, struts. All Struts applications are layered.
7. Static variables are initialized at compile time.
8. All exceptions that are declared in throws clause are checked exception.
9. Check exceptions are caught at compile time in catch block. "He mean't some thing else, but what it sounded was a concept in itself"

And many more hilarious concepts which I no longer remember. It was indeed crash course for me, couldn't grasp any of these new concepts. Where is my ability to learn new concepts ? Have I lost it.

Tuesday, April 20, 2010

Generics

Generics, the new feature of Java. Don't really expect to people to be comfortable specially if they have been working in older java versions. But if, candidate is certified SCJP on Java 5, things change. One can expect at least such fellows to be aware of Generics.

Q: So, you are certified SCJP 5 ?
A: Yes.

Q: That means you must be aware of generics ? One of the features introduced in Java 5.
A: Confidently, yes.

Q: Generics. I am sure you know what it is. What benefit does it provide ?
A: Generics !!! (Now started to look puzzled). Generics makes it easy as generic class can be inherited and allows to re-use code in generic class.

Now we are puzzled how does generic class and inheritance come up in Generics. Probably, there was confusion over question. So repeated it and ensured that interviewee understood its GENERICS no GENERIC CLASS.

Still there was no change in answer. Generic class helps in re-use implementation.

....
...

With some more such responses we were puzzled whether interviewee actually holds SCJP 5 certificate. So much so we ended up searching for SCJP certificate and to our surprise we found one which looked real.

Monday, April 19, 2010

Secret of interface Set: It implements

How does set ensures that set doesn't end up adding same object in a set twice. In my previous blog I mentioned about compile time error.

There is another answer to set's ability to maintain uniqueness.

A: Set uses equals method to compare whether two objects are same or not. (Just as you think this fellow is going in correct direction, read next sentence). All set objects have equals method so objects are compared by set itself.

Q: As you mentioned there is "equals" method in Set and then there is also "equals" method in class String. Suppose I create Set of string which of the "equals" method would be used by Set ? The one in Set and the one in String?

A: Set. Set is implemented in such a way that it calls equals method. Since set itself has "equals" method it is called.

Wow ...... Set is implemented such a way.

Q: Suppose I want to create custom Set class. So I create class CustomSet implements Set. What would I have to do in order to check for duplicates in my CustomSet class.

A: Nothing. Set is implemented in such a way that it will call equals method automatically when new item is added into set..

Q: So, nothing needs to be done for new custom class ?
A: (After some thinking) Nothing needs to be done.

Wow !!! Interface Set is implemented such a way that it automatically calls equals method from add method when ever it is implemented. And guess what interfaces implement secretly thing what you would want.

Wednesday, April 14, 2010

More Synchronized funda from another SCJP

Nowadays I wonder whether synchronized is a keyword of JAVA or not. Because if it were, it would have been clear to people appearing for interview especially those who are SCJP.

Since two threads can't execute different synchronized method at a time. A lock on object is acquired to achieve this. Fair enough, that's what is written in books. Can a synchronized method call another synchronized method ? Consider below example ... can methodA call methodB where both are synchronized ?

class A{

public synchronized void methodA(){

//some operations 
    methodB();
}

public synchronized void methodB(){
//some operations 
}

}

What do you think ? 

Well this is answer from SCJP .... Since lock is already acquired methodB will not be able to acquire it again. It would be blocked sort of deadlock. !!!! 

Tuesday, April 13, 2010

Compile time error at Runtime

Well what is a Set ? Its simple isn't it ? A collection that doesn't contain any duplicate objects.

But then what ensures that set doesn't end up adding same object in a set twice. What do you think ?

Okay, if you are think it is pretty straight answer to this. What do you do when you end up hearing below as answer.

A: Set implementation ensures no duplicates are added. It throws error, compile time error.

Q: Huh, compile time error ?  What if we have a set which stores employee name and program at run time asks for employee names. User enters and name is then added into the set. How will uniqueness of objects be ensured in set , what will happen if user enters same name twice and program tries to add same name (string) in Set again?

A: As I said an error will be thrown at compile time.

OMG !!! This is new one, compile time error is thrown at runtime. I still am trying to understand, how can this happen. Help me, if possible in understanding this concept. Software industry is indeed moving quickly, I hope I would be able to keep up with the pace its changing :).

Tuesday, April 6, 2010

Some interesting concepts

Concepts of a person give a good insight on how person thinks and would be able to think when a problem domain is in front of them. So, often interviews involving entry level include questions comprising of basic concepts. Although, interviewer doesn't except bookish answers he also doesn't except below too. Or do they ?

Some of the questions in one such interview where I was involved and almost started to wonder do I know what I know is correct.


....
....

Q: What is the purpose of finalize method ?
A: Finalize is one of the methods in object class. Flinalize method tells us whether GC (garbage collecter) is running or not.

Whoa !!! interesting ... just wondering why do i need to know the status of GC anyway.

....
....

Q: As you said wait / notify are methods of object. Can I call these methods anywhere I wish or there is any restriction ?
A: They can be called in synchronized block. .... and method also.

...

Q: So can we have static synchronized method ?
A: Let me think !!!! After some thinking ...... I don't think so. Static method is associated with class not instance. Since we don't have an object to have lock on, it is not possible.

Ohhh !!!! Answer with logical explaintion.

....
....

And then best of all 

Q: What are prepared statements ?
A: They are pre-compiled statements, good for performance.

Yeah !!! This fellow knows what prepared statements are. I also thought so. Keep reading.

Q:So why are such statements called pre-compiled statements ?
A: Because the are compiled only once. Suppose we want to execute a query "select * from employee where employeeId=?" Such queries where ? are there and we set values before executing are prepared statements.

Q:Okay, we have ? in query, so our statement would be prepared statement ?
A: Yes

Q: What do you mean with compiling ... in pre-compiler statement ?
A: When we compile java class, query written in java also gets compiled. So we also call it pre-compiled statement. 

Ohh no !!! he did it again another concept.

Q: Oh.. so If we want to fetch all employees from employee table "select * from employee" and compile java class. My query would get compiled first. So I would have pre-complied statement ?
A: Yes 

Whoa !!!! 

....
...

I hope after reading this, you found some thing not to learn from.