Friday 22 February 2013

SOLID - OOPS

Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion.

Single responsibility principle
    an object should have only a single responsibility.

Open/closed principle
    “software entities … should be open for extension, but closed for modification”.

Liskov substitution principle
    “objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program”. See also design by contract.

Interface segregation principle
    “many client-specific interfaces are better than one general-purpose interface.”

Dependency inversion principle
    one should “Depend upon Abstractions. Do not depend upon concretions.”
    Dependency injection is one method of following this principle.

Sunday 17 February 2013

Spring key points

IOC or inversion of control / DI or dependency injection?
                 This Spring interview question is first step towards Spring framework and many interviewer starts Spring interview from this question. As the name implies Inversion of control means now we have inverted the control of creating the object from our own using new operator to container or framework. Now it’s the responsibility of container to create object as required. We maintain one xml file where we configure our components, services, all the classes and their property. We just need to mention which service is needed by which component and container will create the object for us. This concept is known as dependency injection because all object dependency (resources) is injected into it by framework.
Example:
  <bean id="createNewStock" class="springexample.stockMarket.CreateNewStockAccont">
        <property name="newBid"/>
  </bean>
In this example CreateNewStockAccont class contain getter and setter for newBid and container will instantiate newBid and set the value automatically when it is used. This whole process is also called wiring in Spring and by using annotation it can be done automatically by Spring, refereed as auto-wiring of bean in Spring.
------------------------------------------------------------------------------------------------------------
AOP?
      The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. AOP is a programming technique that allows developer to modularize crosscutting concerns,  that cuts across the typical divisions of responsibility, such as logging and transaction management. Spring AOP, aspects are implemented using regular classes or regular classes annotated with the @Aspect annotation
Jointpoint?
    A joinpoint is a point in the execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.
Advice?
    Advice is the implementation of an aspect. It is something like telling your application of a new behavior. Generally, and advice is inserted into an application at joinpoints.
Pointcut?
    A pointcut is something that defines at what joinpoints an advice should be applied. Advices can be applied at any joinpoint that is supported by the AOP framework. These Pointcuts allow you to specify where the advice can be applied.
------------------------------------------------------------------------------------------------------------
Core container module?
       This module is provides the fundamental functionality of the spring framework. In this module BeanFactory is the heart of any spring-based application. The entire framework was built on the top of this module. This module makes the Spring container.
------------------------------------------------------------------------------------------------------------
Application context module?
       The Application context module makes spring a framework. This module extends the concept of BeanFactory, providing support for internationalization (I18N) messages, application lifecycle events, and validation. This module also supplies many enterprise services such JNDI access, EJB integration, remoting, and scheduling. It also provides support to other framework.

OOPs

The four main concepts are involved in OOP:
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
Abstraction is hiding of information from others. In case of Java, its hiding implementation of a method/ object. For example if an interface is available to outer world, so end user won’t know about implementing class details. This is just a kind of abstraction. Abstraction could be anywhere in java, wherever we hide some information, we call it abstraction.
Encapsulation is combining data and method along with implementation hiding within the class. Implementation hiding is done with the help of access modifiers (public, protected, package, private).
Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. Although inheritance may sometimes imply (especially in Java, where the keyword for inheritance is extends) that you are going to add new methods to the interface, that’s not necessarily true. The second and more important way to differentiate your new class is to change the behavior of an existing base-class method. This is referred to as overriding that method.
Polymorphism is something like one name many forms. Polymorphism is also known as dynamic binding or late binding or run-time binding.

---------------------------------------------------------------------------------------------------------------------------
Inheritance?

Definition 1:
Java Inheritance defines an is-a relationship between a superclass and its subclasses. This means that an object of a subclass can be used wherever an object of the superclass can be used. Class Inheritance in java mechanism is used to build new classes from existing classes.
Definition 2:
Inheritance means to acquire property of others. It also means that we can use code of other functions and methods. Means Code reusability. In this object are defines by classes that are super class and which classes uses property of that class is known as sub class. It is very useful in Object Oriented Languages.
-------------------------------------------------------------------------------------------------------------------------
What is Association?
Association is a relationship between two classes. In this relationship the object of one instance perform an action on behalf of the other class. The typical behaviour can be invoking the method of other class and using the member of the other class.

   
public class MyMainClass
{
 public void init()
  {
    new OtherClass.init();
  }
}
-------------------------------------------------------------------------------------------------------------------------
What is Aggregation?

Aggregation has a relationship between two classes. In this relationship the object of one class is a member of the other class. Aggregation always insists for a direction.
   
public class MyMainClass
{
  OtherClass otherClassObj = new OtherClass();
}
-------------------------------------------------------------------------------------------------------------------------
What is Composition?
Composition is a special type of aggregation relationship with a difference that its the compulsion for the OtherClass object to exist for the existence of MyMainClass.

import java.util.ArrayList;

public class Aggregation
{
 public static void main(String[] args)
  {
    MusicPlayer player = new MusicPlayer();
    Car car = new Car();
    car.addPlayer(player);
  }
}

Serialization

1) A class should implement java.io.Serializable interface. (gives compiler an indication)
2) SerialVersionUID JVM generates its based upon structure of class which depends upon interfaces a class implements and several other factors. SerialVersionUID is used for version control of object. serialVersionUID mismatch leads to java.io.InvalidClassException.
3) Externalizable - Flexibility to control the serialization. using right/readExternal()
4) serializing not include some of the members can be achived using static variable or transient variable.
5) Custom serialization are possible, But should take care of Provate methods, Overloading and overriding, Since the private methods reading possible only from JVM, Still what is the need for custom serialization.
6)  To avoid java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.

Monday 11 February 2013

Java 1.7 features.

    1) JDBC 4.1 more efficient introduced with the following features:
        The ability to use a try-with-resources statement to automatically close resources of type Connection, ResultSet, and Statement
        RowSet 1.1: The introduction of the RowSetFactory interface and the RowSetProvider class, which enable you to create all types of row sets supported by your JDBC driver.
    2) String objects can be as Switch literals.
        case "ONE": sop("one"); break;
    3) Automatic Type referece during Generic instance creation.
        Map> sample = HashMap<>(); 
        <> - operator called as diamond operator.
    4) try statement defining resources. No need of finally method to close this resources.
        try(file reader){
            return (readline)
        }
    5) Catch block to handle multiple exceptions.
        catch( arrayourofboune | numberformatexception | ex)
        since we cant use the main exception with in multple exception.
    6) JVM enhancement.

    7) IO - Zip File System Provider, Custom file system provider, java.nio.*, etc