Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, September 13, 2017

Purpose of marker interfaces in Java

Marker interface is an empty interface (no field or methods). Examples of marker interface are Serializable, Clonnable, Remote and ThreadSafe interfaces. 
Note: Marker interface is used to inform the JVM that the classes implementing them will have some special behavior.
 In java we have following four major marker interfaces:
  • Searilizable interface
  • Cloneable interface
  • Remote interface
  • ThreadSafe interface

public interface Serializable 
{
  // nothing here
}

Q. Marker interface won't contain any method then how the objects will get that special ability?
Ans. JVM is responsible to provide required ability in marker interfaces

Wednesday, February 22, 2017

How to create thread safe classes in Java


Thread safety in java is the process to make our program safe to use in multi threaded environment, there are different ways through which we can make our program thread safe.

1) Synchronization is the easiest and most widely used tool for thread safety in java.

2) Immutable objects are by default thread safe because there state can not be modified once created. For example, String class is immutable in java which inherently thread-safe.

3) Use of Atomic Wrapper classes from java.util.concurrent.atomic package. Atomic operations in Java are thread-safe. For example AtomicInteger.

4) Use of locks from java.util.concurrent.locks package.

5) Using volatile keyword with variables to make every thread read the data from memory, not read from thread cache. 

6) Using thread safe classes for thread safety. Example of thread-safe class in Java: Vector, Hashtable, ConcurrentHashMap, String etc.


7)  Locking is one way of achieving thread-safety in Java.

Friday, January 29, 2016

OOPS

Object Oriented Programming Concepts


What are the principle concepts of OOPS?
There are four principle concepts upon which object oriented design and programming rest. They are:
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
  • What is Abstraction?
    Abstraction refers to the act of representing essential features without including the background details or explanations. Abstraction focus on what the object does instead of how it does it.
    What is Encapsulation?
    Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object. Encapsulation means hiding the internal details or mechanics of how an object does something.
    What is Inheritance?
    Inheritance is the process by which objects of one class acquire the properties of objects of another class.
    What is Polymorphism?
    Polymorphism is briefly described as “one interface, many implementations.” Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
    What is a marker interface?
    Marker interfaces are those which do not declare any required methods, but signify their compatibility with certain operations. The java.io.Serializable interface and Cloneable are typical marker interfaces. These do not contain any methods, but classes must implement this interface in order to be serialized and de-serialized.

    Characteristics of a Good API

    Characteristics of a Good API


    • Easy to learn
    • Easy to use, even without documentation
    • Hard to misuse
    • Easy to read and maintain code that uses it
    • Sufficiently powerful to satisfy requirements
    • Easy to extend
    • Appropriate to audience

    Singleton Pattern

    Singleton Pattern


    1. What is singleton pattern?
    The singleton pattern is a design pattern used to create single instance of class (only one object) thought out the application.
    2. How do you implement singleton pattern in java?
    There are two ways to implement singleton pattern in java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class Singleton {
    private static Singleton singleInstance;
    // Make constructor as private to ensure that not able to instantiate a class outside
    private Singleton() {
    }
    public static Singleton getInstance() {
    if (singleInstance == null) {
    singleInstance = new Singleton();
    }
    return singleInstance;
    }
    another way:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public class Singleton {
    private static Singleton singleInstance = new Singleton();
    // Make constructor as private
    private Singleton() {
    }
    public static Singleton getInstance() {
    return singleInstance;
    }
    3. How do you make sure that it works in multi threaded environment?
    This is simple and thread-safe singleton implementation.
    1
    2
    3
    4
    5
    6
    7
    public static synchronized Singleton getInstance()
    {
    if (singleInstance == null) {
    singleInstance = new Singleton();
    }
    return singleInstance;
    }
    Synchronizing block (double-checked locking) instead of entire method for better performance, but unfortunately, it does not work.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // Double-checked locking -- don't use
    public static Singleton getInstance() {
    if (singleInstance == null) {
    synchronized(Singleton.class) {
    if (singleInstance == null) {
    singleInstance = new Singleton();
    }
    }
    }
    return singleInstance;
    }
    An alternative thread-safe singleton implementation:
    This is simple, fast, and thread-safe singleton implementation
    1
    2
    3
    4
    5
    6
    7
    public class Singleton {
    public final static Singleton INSTANCE = new Singleton();
    private Singleton() {
    // Exists only to defeat instantiation.
    }
    }
    4. How do you prevent creating another instance of Singleton using clone() method?
    We can create another instance of class as shown below.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class Clone
    {
    public static void main(String args[])
    throws Exception
    {
    // Get a singleton
    Singleton obj = Singleton.getInstance();
    // Let's clone the object
    Singleton clone = (Singleton) obj.clone();
    }
    }
    We can prevent another instance of class by overriding the Object’s clone method which throws a CloneNotSupportedException exception.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    public class Singleton {
    private static Singleton singleInstance;
    // Make constructor as private
    private Singleton() {
    }
    public static Singleton getInstance() {
    if (singleInstance == null) {
    singleInstance = new Singleton();
    }
    return singleInstance;
    }
    // Override the Object’s clone method and throw CloneNotSupportedException exception
    public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
    }
    5. How do you prevent creating another instance of Singleton during serialization?
    If the Singleton class implements the java.io.Serializable interface, when a singleton is serialized and then deserialized more than once, there will be multiple instances of Singleton created. In order to avoid this the readResolve method should be overridden and all instance fields should be declared as transient (to prevent a serialization attack) . See Serializable () and readResolve Method () in javadocs.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class Singleton implements java.io.Serializable
    {
    ...
    // This method is called immediately after an object of this class is deserialized.
    // This method returns the singleton instance.
    private Object readResolve() {
    return getInstance();
    }
    }
    6. How do you prevent creating another instance of Singleton using reflection?

    7. What are the downsides to using a Singleton Class with `private` Constructor?

    Single Responsibility Principle Violation, Because it handles it's own creation in addition to whatever responsibility it handles, it violates SRP.