Interview Questions

Design Patterns

Java Best Practice

In this article, we will discuss about certain best practices to be followed while developing programs or applications using JAVA.

Variable Declaration
When the variables are defined, please make sure you give meaningful names. For example

String d1 = "RC221";

Instead of naming a device as d1, we can give a meaningful name applicable for the context.

String deviceName = "RC221";

Whenever you create a method inside the class, declare all the method variables and then start the program code logic. This will increase the readability.

Abstract Class and Interface

An Abstract class in java is a class for which objects cannot be created. It has the following properties.

1. It may contain zero or more abstract methods.
2. Abstract methods should be preceded with the keyword abstract.
3. It may or may not contain concrete methods.
4. Abstract classes cannot be instantiated. You cannot create objects for abstract class.
5. The concrete subclass which extends the abstract class, should implement all the abstract methods present in that extended abstract class.
6. It can have both private and public constructors.

Java - Observer Design Pattern

In this article let’s discuss about Observer Design Pattern.

The Observer pattern clearly depicts the Publisher-Subscriber model. A publisher is one who provides data and notifies it to the list of subscribers who have subscribed for the same to that publisher. A simple example is Newspaper. Whenever a new edition is published by the publisher it will be given to the subscribers who have them subscribed for the same.

Factory Design Pattern

In this article let’s discuss in brief about Factory Design Pattern

In an Object Oriented Programming language like java, we usually deal with classes, creating objects for them and executing operations in those objects. To create any object we will use the new keyword and it will work fine. Consider a scenario where you need to create an object based on the given input, from a set of objects available.

Let’s take up an example of an abstract class Car and set of concrete classes extending it.

package in.techdive.factory;

Singleton Design Pattern

In this article lets discuss about Singleton Design Pattern.

In many applications there may be a need to create only one object for a class so that the object will be shared by the entire application code. In this case we need to make sure not more than one object to be created.

We can do this by making the constructor the class as private and returning the object using a static method. Have a look at the following Singleton class.


public class Singleton {
        private static Singleton s = null;
        private Singleton()