What is Joint point and Point cut?

This is not really a spring interview questions I would say an AOP one. AOP is another popular programming concept which complements OOPS. Join point is an opportunity within code for which we can apply an aspect. In Spring AOP, a join point always represents a method execution.

Pointcut: a predicate that matches join points. A point cut is something that defines at what join points an advice should be applied

These spring interview Questions are not very difficult in nature and focused on spring fundamentals rather than focusing on advanced feature of session management, sprint security, authentication etc. we will cover of those question on some other interview article. I would also suggest that share some spring questions asked to you guys during interview and then I can put together those with their answers for quick reference of everybody.

When you go out to a restaurant, you look at a menu and see several options to choose from. You can order one or more of any of the items on the menu. But until you actually order them, they are just “opportunities to dine”. Once you place the order and the waiter brings it to your table, it’s a meal.

Join points are the options on the menu and pointcuts are the items you select. A joinpoint is an opportunity within code for you to apply an aspect…just an opportunity. Once you take that opportunity and select one or more joinpoints and apply an aspect to them, you’ve got a pointcut.

Spring Design Patterns

1. MVC – The advantage with Spring MVC is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers.
Model View Controller – The advantage with Spring MVC is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers. One thing to note is that the controller is only required to return a logical view name, and the view selection is left to a separate ViewResolver. This makes it easier to reuse controllers for different view technologies.

2. Front controller – Spring provides “DispatcherServlet” to ensure an incoming request gets dispatched to your controllers.
Front Controller – Spring provides DispatcherServlet to ensure an incoming request gets dispatched to your controllers.

3. View Helper – Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views.

4. Singleton – Beans defined in spring config files are singletons by default.
Singleton – by default, beans defined in spring config file (xml) are only created once. No matter how many calls were made using getBean() method, it will always have only one bean. This is because, by default all beans in spring are singletons.
This can be overridden by using Prototype bean scope.Then spring will create a new bean object for every request.

5. Prototype – Instance type can be prototype.

6. Factory – Used for loading beans through BeanFactory and Application context.
Factory – Spring uses factory pattern to create objects of beans using Application Context reference.
// Spring uses factory pattern to create instances of the objects
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(“spring.xml”));
Triangle triangle = (Triangle) factory.getBean(“triangle”);
triangle.draw();

7. Builder – Spring provides programmatic means of constructing BeanDefinitions using the builder pattern through Class “BeanDefinitionBuilder”.

8. Template – Used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc..). For example JdbcTemplate, JmsTemplate, JpaTemplate.

9. Proxy – Used in AOP & Remoting.

10. DI/IOC – It is central to the whole BeanFactory/ApplicationContext stuff.
Dependency injection/ or IoC (inversion of control) – Is the main principle behind decoupling process that Spring does

11. View Helper – Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views

Advantage of Hibernate over JDBC

1) Hibernate is data base independent, same code will work for all data bases like ORACLE,MySQL ,SQLServer etc.
In case of JDBC query must be data base specific.

2) As Hibernate is set of Objects , you don’t need to learn SQL language.
You can treat TABLE as a Object .
In case of JDBC you need to learn SQL.

3) Don’t need Query tuning in case of Hibernate. If you use Criteria Quires in Hibernate then hibernate automatically tuned your query and return best result with performance.
In case of JDBC you need to tune your queries.

4) You will get benefit of Cache. Hibernate support two level of cache. First level and 2nd level. So you can store your data into Cache for better performance.
In case of JDBC you need to implement your java cache .
5) Hibernate supports Query cache and It will provide the statistics about your query and database status.
JDBC Not provides any statistics.

6) Development fast in case of Hibernate because you don’t need to write queries.

7) No need to create any connection pool in case of Hibernate. You can use c3p0.
In case of JDBC you need to write your own connection pool.

8) In the xml file you can see all the relations between tables in case of Hibernate. Easy readability.

9) You can load your objects on start up using lazy=false in case of Hibernate.
JDBC Don’t have such support.

10 ) Hibernate Supports automatic versioning of rows but JDBC Not.

Implicit and Explicit Intent Example

Android OS Activity have two types intent.

1) Implicit Intent::

Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.

Button openBrowser= (Button)findViewById(R.id.bOpenBrowser);

bOpenBrowser.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.pars(“http://www.google.com”));
startActivity(webIntent);

}

});

2) Explicit Intent::

Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application.

Button openGame= (Button)findViewById(R.id.bOpenGame);

openGame.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent launchExplicitIntent = new Intent(this, Game.class);
startActivity(launchExplicitIntent);

}

});

Where do android developers hang out online?

Developer Resource Forums

    http://StackOverflow.com
    http://Forums.DevShed.com
    http://www.webdeveloper.com/forum/
    http://GitHub.com
    http://forums.devarticles.com/

Android Developer Forums

    http://www.anddev.org/
    http://forum.xda-developers.com/
    https://groups.google.com/group/android-developers?pli=1
    http://forum.androidcentral.com/
    http://androidforums.com/application-development/
    http://www.androidpit.com/en/android/forum/category/1004010/Android-Developer-Forum

XDA Developer Forums

    http://forum.xda-developers.com/

Game Developer Forums

    http://forums.indiegamer.com/
    http://www.gamedev.net/index
    http://www.devmaster.net/forums/
    http://gmc.yoyogames.com/

finalize(), finally and final in Java

There’s some confusion in the mind of an intermediate level programmer about finalize(),finally and final in java. So to remove that confusion from each and everyone’s mind I thought of writing a post on it.

finalize() : A Method
finally : A Clause in Try-Catch
final : Keyword in java

finalize :

It is called by the garbage collecter on an object by garbage collection when it knows that there is no references on the object. The finalize() method is defined in the java.lang.Object class and its modifier is defined as protected. The finalize() method is not public because it should only be invoked by JVM and not by anyone else and protected so that it can be overridden by the subclasses.

You can override the finalize() method and the declaration of the method should look like this :

protected void finalize() throws throwable

Ex.

The below class opens the file when constructed :

class OpenAFile {
FileInputStream aFile = null;
OpenAFile(String filename) {
try {
aFile = new FileInputStream(filename);
} catch (java.io.FileNotFoundException e) {
System.err.println(“Could not open file ” + filename);
}
}
}

As a part of good programming practice, the open file should be closed :

protected void finalize () throws throwable {
if (aFile != null) {
aFile.close();
aFile = null;
}
}

It is important to understand that finalize() is only called just prior to garbage collection. It is not called when an object goes out-of-scope, for example. This means program should provide other means of releasing system resources, etc., used by the object. It must not rely on finalize() for normal program operation.

finally :

The finally clause defines a code that always executes, regardless of whether the exception was caught. The following sample code is taken from the book by Frank Yellin:

try {
startFaucet();
waterLawn();
} catch (Exception e) {
logProblem(e);
} finally {
stopFaucet();
}

In the above example the Faucet is turned off regardless of whether the exception was caught or not. The code inside the braces after try is called the protected code.

The only thing that stop finally from executing are virtual machine shutdowns i.e. System.exit method etc.

The final keyword in Java

Like other keywords in java, final is also a keyword used in several different contexts to define an entity which cannot later be changed.

Here in this tutorial we will be dealing with final classes, final methods, final variables and also I will be covering blank final variables.

    final classes

The Java Programming language permits us to apply the keyword final to classes. If the class is made final then it cannot be sub-classed.

Ex :

The java.lang.String is a final class. This is done for security reasons, because it ensures that if the method is referenced as String then the method is a definite string of class String and not a string of class which has sub-classed String class.

    final methods

Like class we can also mark methods as final. Methods that are marked as final cannot be overridden in any case. For security reasons only you must make the method as final if the method has implementation which you don’t want others to change.

Methods declared as final can be optimized. The compiler can generate a code that causes a direct call to the method, rather than invoking it the usual way i.e during the run-time.

    final variables

If a variable is marked as final then the value of that variable cannot be changed i.e final keyword when used with a variable makes it a constant. And if you try to change the value of that variable during the course of your program the compiler will give you an error.

NOTE: If you mark variable of a reference type as final, that variable cannot refer to any other object. However, you can change the object’s contents, because only the reference itself is final.

    Blank final variables

A blank final variable is a variable that is not initialized during its declaration. The initialization is delayed. A blank final instance variable must be assigned in a constructor, but it can be set only once. A blank final variable that is local variable can be set at any time in the body of the method, but it can be set only once.

The following code fragment is an example of how a blank final variable can be used in class.

public class Customer{
private final long customerID;

public Customer(){
customerID = createID();
}

public long getID(){
return customerID;
}

public long createID(){
return … // generates new ID
}
… // more declarations
}

Externalizable vs Seriablizable

Externalizable is an interface that enables you to define custom rules and your own mechanism for serialization. protocol and provides out of the box serialization capabilities.

Externalizable extends Seriablizable.

Implement writeExternal and readExternal methods of the Externalizable interface and create your own contract / protocol for serialization.

Saving the state of the super types is responsibility of the implementing class.

You might have seen in my previous article on how to customize the default implementation of Seriablizable. These two methods readExternal and writeExternal (Externalizable) supersedes this customized implementation of readObject and writeObject.

In object de-serialization (reconsturction) the public no-argument constructor is used to reconstruct the object. In case of Seriablizable, instead of using constructor, the object is re-constructed using data read from ObjectInputStream.

The above point subsequently mandates that the Externalizable object must have a public no-argument constructor. In the case of Seriablizable it is not mandatory.

Behaviour of writeReplace and readResolve methods are same for both Seriablizable and Externalizable objects. writeReplace allows to nominate a replacement object to be written to the stream. readResolve method allows to designate a replacement object for the object just read from the stream.

In most real time scenarios, you can use Seriablizable and write your own custom implementation for serialization by providing readObject and writeObject.

You may need Externalizable,

1. If you are not happy with the way java writes/reads objects from stream.

2. Special handling for super types on object construction during serialization.