Home / Questions / Question 1
Explanatory Question

Question 1

👁 74 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Question 1

(a) Define encapsulation.

Answer

The wrapping of data and methods that operate on that data into a single unit is called Encapsulation.

(b) Explain the purpose of using a 'new' keyword in a Java program.

Answer

The new keyword instantiates an array or a class by dynamically allocating memory for it at runtime and returning a reference to that memory.

For example:

Employee emp = new Employee();

Here we are creating an object of class Employee using the new keyword.

(c) What are literals?

Answer

Any constant value which can be assigned to the variable is called as literal. There are different types of literals like integer literals, floating point literals, character literals, String literals, boolean literals and null literals.

(d) Mention the types of access specifiers.

Answer

There are four types of access specifiers:

  1. default
  2. public
  3. private
  4. protected

(e) What is constructor overloading?

Answer

Constructor overloading is a technique in Java through which a class can have more than one constructor with different parameter lists. The different constructors of the class are differentiated by the compiler using the number of parameters in the list and their types.

For example, a class Employee can have 3 constructors as shown below:

Employee(long empId)
Employee(String name, double salary)
Employee(long empId, String name)

Due to constructor overloading, all the 3 constructors are valid for Employee class.