Design Pattern Interview Questions

What is design pattern? Have you used any design pattern in your code ?

Design patterns are tried and tested way to solve particular design issues by various programmers in the world. Design patterns are extension of code reuse.

Can you name few design patterns used in standard JDK library?
  1. Decorator design pattern which is used in various Java IO classes.

  2. Singleton pattern which is used during runtime.

  3. Calendar and various other classes.

  4. Factory pattern which is used along with various Immutable classes like Boolean e.g. Boolean.valueOf.

  5. Observer pattern which is used in Swing and many event listener frameworks.

What is Singleton design pattern in Java ?

Singleton pattern focuses on sharing expensive object in whole system. Only one instance of a particular class is maintained in the whole application which is shared by all modules. Java.lang.Runtime is a common example of Singleton design pattern.

What is the main benefit of using factory pattern? Where do you use it?

Factory pattern's main benefit is the increased level of encapsulation while creating objects. If one uses Factory to create object one can later replace original implementation of Products or classes with more advanced and high performance implementation without any change on client layer.

What is Observer design pattern in Java?

Observer design pattern is based on communicating changes in state of object to observers so that they can take there action. Simple example is a weather system where change in weather must be reflected in 'Views' to show to public

What is Decorator design pattern?

Decorator pattern enhances capability of individual object. Java IO uses decorator pattern extensively and classical example is Buffered class say for instance BufferedReader and BufferedWriter which enhances Reader and Writer objects to perform Buffer level reading and writing for improved performance .

When do you overload a method in Java and when do you override it ?

Rather a simple question for experienced designer in Java. If one sees implementation of a class has different ways of doing certain things then overriding is the way to go while overloading is doing same thing but with different inputs. Method signature varies in case of overloading but not in case of overriding in java.

Give an example where we will prefer abstract class over interface.

Both interface and abstract class follow "writing code for interface then implementation" design principle which adds flexibility in code which is quite important to tackle with changing requirements. Here are some pointers which help you to answer this question:

  • In Java we can only extend one class but implement multiple interfaces. So if we extend a class we may lost our chance of extending another class.

  • Interfaces are used to represent adjective or behavior e.g. Runnable, Clonable, Serializable etc. So if we use an abstract class to represent behavior our class won't be Runnable and Clonable at same time because we cannot extend two classes in Java but if we use interface our class can have multiple behavior at the same time.

  • At a critical time application prefers abstract class as it is slightly faster than interface.

  • If there is a genuine common behavior across the inheritance hierarchy which can be coded better at one place, then abstract class is preferred. Sometimes interface and abstract class can work together also where we define function in interface whereas default functionality in abstract class.

Learn about Java in Detail