Encapsulation Interview Questions

What is Encapsulation?
  • Encapsulation is the technique of making the fields in a class private and providing access to these fields with the help of public methods.

  • If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class.

  • Therefore encapsulation is also referred to as data hiding.

What is the primary benefit of encapsulation?

The main benefit of encapsulation is the ability to modify the implemented code without breaking the code of others who use our code. It also provides us with maintainability, flexibility and extensibility to our code.

What is an interface?

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Mention some features of interface.

It includes -

  • Interface cannot be instantiated.

  • An interface does not contain any constructors.

  • All the methods in an interface are abstract.

What is the difference between encapsulation and abstraction?
  1. Abstraction solves the problem at design level while encapsulation solves the problem at implementation level.

  2. Abstraction is used for hiding the unwanted data and provide only the required data. On the other hand encapsulation means hiding the code and data into a single unit to protect the data from outside world.

  3. Abstraction lets you focus on what the object does instead of how it does it while Encapsulation means hiding the internal details or mechanics of how an object does something.

  4. For example: Outer Look of a Television i.e. it has a display screen and channel buttons to change channel explains 'abstraction' but inner implementation detail of a television i.e. how CRT and display screen are connected with each other using different circuits explains 'encapsulation'.

What are the features of encapsulation?
  • Encapsulation means combining the data of our application and its manipulation at one place.

  • Encapsulation allows the state of an object to be accessed and modified through behaviour.

  • It reduces the coupling of modules and increases the cohesion inside them.

Explain in detail encapsulation in Java?
  • Encapsulation is nothing but protecting anything which is prone to change. Rational behind encapsulation is that if any functionality which is well encapsulated in code i.e maintained in just one place and not scattered around code is easy to change.

  • This can be better explained with a simple example of encapsulation in Java. We all know that constructor is used to create object in Java and constructor can accept argument. Suppose we have a class 'Loan' which has a constructor and in various classes we have created instance of 'loan' using this constructor. Now requirements will change and you need to include 'age of borrower' as well while taking loan.

  • Since this code is not well encapsulated i.e. not confined in one place you need to change at every place where you are calling this constructor i.e. for one change you need to modify several files instead of just one file which is more error prone and tedious. Though it can be done with refactoring feature of advanced IDE it would prove better if we only need to make change at one place.

  • This is possible if we encapsulate 'Loan' creation logic in one method say 'createLoan()'. The code written for client will call this method and this method internally creates 'Loan' object. in this case you only need to modify this method instead of the whole client code.

Give an example of Encapsulation in Java.
What are the advantages of using encapsulation in Java and OOPS?

Below mentioned are few advantages of using encapsulation while writing code in Java or any Object oriented programming language:

  1. Encapsulated Code is more flexible and easy to change with inception of new requirements.

  2. Encapsulation in Java makes unit testing easy.

  3. Encapsulation in Java allows you to control who can access what.

  4. Encapsulation also helps to write immutable class in Java which is a good choice in multi-threading environment.

  5. Encapsulation reduces coupling of modules and increases cohesion inside a module because all the pieces of one thing are encapsulated in one place.

  6. Encapsulation allows you to change one part of code without affecting other part of code.


What should you encapsulate in code?

Anything which can be changed or which is more likely to be changed in near future is candidate of encapsulation. This also helps to write more specific and cohesive code. For instance object creation code, code which can be improved in future like sorting and searching logic.

Mention some important points about encapsulation in Java.
  1. "Whatever changes encapsulate it" is a famous design principle.

  2. Encapsulation helps in loose coupling and high cohesion of code.

  3. Encapsulation in Java is achieved using access modifiers private, protected and public.

  4. 'Factory pattern' and 'singleton pattern' in Java makes good use of encapsulation.

Explain design pattern based on encapsulation in Java.
  • Many design patterns in Java uses encapsulation concept, one of them is 'factory pattern' which is used to create objects.

  • 'Factory pattern' is a better choice then 'new' operator for creating object of those classes whose creation logic can vary and also for creating different implementations of same interface.

  • 'BorderFactory class' of JDK is a good example of encapsulation in Java which creates different types of 'border' and encapsulates creation logic of border.

  • 'Singleton pattern' in Java also encapsulates how you create instance by providing getInstance() method.

  • Since object is created inside one class and not from any other place in code you can easily change how you create object without effecting other part of code.

What are the benefits of encapsulation?
  • The fields of a class can be made read-only or write-only.

  • A class can have total control over what is stored in its fields.

  • The users of a class do not know how the class stores its data. A class can change the datatype of a field and users of the class do not need to make any changes to their code.

Give an example of how to achieve encapsulation in Java?

To achieve encapsulation in Java:

  • Declare the variables of a class as 'private'.

  • Provide public setter and getter methods to modify and view the variable's values.

Below given is an example that demonstrates how to achieve Encapsulation in Java:

How can the variables of the EncapTest class be accessed?

The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class. Normally, these methods are referred as getters and setters.

Therefore any class that wants to access the variables should access them through these getters and setters. The variables of the EncapTest class can be accessed as shown below:

This would produce the following result:


Learn More about Encapsulation