Archive

When not to use Inheritance in Java

February 29, 2012 - 10:49 pm

Inheritance is definitely an important aspect when it comes to developing Java/JEE applications. The advantages as everybody who knows OOPS knows that is the increased code reuse. Moreover, inheritance also brings run time polymorphism in picture where we can invoke objects on the super-type reference irrespective of what sub-type object is being referenced by the super-type.

Java : Inheritance and Coupling

But an infrequently mentioned aspect of OOPS is Coupling. Coupling refers to the association/dependency of classes on each other. It is a good OOPS design practice to have less coupling within the classes. The decreased coupling results in better maintainability of the application.… Continue reading

Running multiple instances of tomcat on linux

February 28, 2012 - 10:49 pm

Usually we come across situations where we need to run multiple instances of tomcat server in order to run different applications independent of each other. The situation where this is required is:

1) The tomcat server has been modified according to one particular application and it is not easy segregate the server and application so that other legacy/third-party application can be deployed on this tomcat server.

2) Two different user categories will be using the two different URL’s to access the server and this is a company/security requirement or one application has to run on a different port on the server for firewall issues.

Steps to configure multiple instances of tomcat on linux

For running two different tomcat servers, install/unzip tomcat into two different directories. I am assuming them to be /usr/tmp/tomcat1 and /usr/tmp/tomcat2. Deploy the corresponding web applications in to the webapps folder of the tomcat servers.… Continue reading

Core Java Interview Questions

- 10:49 am

Following are some of the core java interview questions which are asked by interviewers. These are from my personal experience. If the core java interview is telephonic, then these questions are asked more frequently as the interviewer can not ask programming questions in that case.

Core Java Interview is different from those of web based basically because of the requirements of the project. Some interviewers also ask core java interview questions in web applications because they expect the candidate to know the language concepts.

1) If the methodA in class A specifies FileNotFoundException in the throws clause then can the methodB which is overriding methodA and is defined in class B specify IOException in the throws clause?
Ans: The overriding method can not widen the checked exception.

2) What is the difference between checked and unchecked exceptions. Give some examples?
Ans: Checked exceptions are forced by compiler to be either caught or specified as the programmer doesn’t have any control over them. Unchecked exceptions are not forced to be caught or specifies as they are raised because of programming issues.

3) What is the purpose of finally block?
Ans: It is called every time irrelevant of whether the exception is raised or not. The purpose is to execute any clean up code in all scenarios.

4) If an exception occurs in the finally block, what will happen?
Ans: The exception if unchecked, gets propagated as would any unchecked exception in a method. If the exception being raised from finally block is checked then it is forced to be caught or specified.

5) Will the finally block run even if there is a return statement being executed from the try or catch block?
Ans: Yes finally block will run in all cases except 1) If JVM crashes 2)System.exit()… Continue reading

Spring Interview Questions and Answers

- 5:27 am

Spring Framework is being widely used and the following Spring Interview Questions can be used for prepare for your next Spring Interview. The answers for these Spring Interview Questions have not been provided as one can easily search and get the answers to them.

1) What is Dependency Injection and how does it relate to Inversion of Control?

2) What are the different types of Inversion… Continue reading

How to find the number of rows in ResultSet

- 5:20 am

As you would have noticed that the ResultSet class in Java doesn’t have a method to get the number of rows retrieved by a ResultSet after executing the SQL query. So if you need to perform some operations based on how many database records have been retrieved by the ResultSet, one should use the following methodology.

We need to iterate over the ResultSet instance and keep on checking if… Continue reading

How to Configure Liferay Database

February 27, 2012 - 10:51 pm

If you have to develop a Liferay portlet which will interact with the database (be it MySQL, Oracle, MS SQL Server), you need to specify the database URL, username and password before creating connections with the liferay database. The best part of using a database with Liferay is that Liferay supports a large number of database systems and hence you have lot options at your hand and need not be tied on only one database vendor.

Also, by default Liferay uses HyperSonic database as Liferay database and hypersonic is a file system based database but is not recommended for production systems.

And if you are looking to configure multiple database systems with Liferay then there is nothing that liferay provides out of the box to use multiple databases with liferay. In order to have multiple liferay database system, you would need to follow some kind of work around. A common idea that comes to my mind is:

Say you have 3 data sources to be used with liferay. The data source which has major data stored for your portlets can be configured as shown in the configuration steps. For other data sources, one can use JDBC or ORM tools to perform operations. This way one can have multiple data sources for single portal installation.

Usually the Liferay Database portlets are created using the service builder tool of Liferay which is available as an ANT build target in the plugins SDK that ships with the Liferay portal.

If you are seeking information about how to install a database system which can be used as Liferay Database then read the installation guide of corresponding database system be it Oracle, MySQL, DB2 or MS SQL Server.

Changes for defining custom Liferay Database

Where to make changes

Once you have manually created the liferay database in mysql for your portlet application, it can be integrated with liferay using the portal-ext.properties file present in the $LIFERAY_HOME/$TOMCAT_HOME/webapps/ROOT/WEB-INF/classes folder.
Here LIFERAY_HOME represents the folder where liferay is installed and TOMCAT_HOME represents the directory where tomcat server has been installed.… Continue reading

How HashMap works internally in Java

February 26, 2012 - 4:42 pm

HashMap is very widely used collection class in Java. It can store key value pairs so that the value objects can retrieved with time complexity of O(1) because of the hashcode. HashMap makes use of hashcode() and equals() method the key objects to determine the storage and retrieval of value objects. In this HashMap tutorial, we will see how HashMap works internally in Java.

This should give you an… Continue reading