Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views3 pages

Constructor Notes

A constructor is a special member method used to initialize objects of a class, sharing the same name as the class. There are two types of constructors: parameterized and non-parameterized, and constructors cannot have default arguments in Java. Key features include having no return type and being implicitly called during object creation, while constructors differ from methods in naming, return types, and inheritance.

Uploaded by

hecin31712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Constructor Notes

A constructor is a special member method used to initialize objects of a class, sharing the same name as the class. There are two types of constructors: parameterized and non-parameterized, and constructors cannot have default arguments in Java. Key features include having no return type and being implicitly called during object creation, while constructors differ from methods in naming, return types, and inheritance.

Uploaded by

hecin31712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Answer the following

Question 1

What is meant by a constructor?

A constructor is a member method that is written with the same name as the class name
and is used to initialize the data members or instance variables. It is invoked at the time of
creating any object of the class. For example:

Employee emp = new Employee();


Here, Employee() is invoking a default constructor.

Question 2

Name the different types of constructors used in a class program.

1. Parameterised constructor
2. Non-parameterised constructor

Question 3

Why do we need a constructor as a class member?

A constructor is used to initialize the objects of the class with a legal initial value.

Question 4

Explain the following terms:

(a) Constructor with default argument

Java specification doesn't support default arguments in methods so Constructor with


default argument cannot be written in Java.

(b) Parameterised constructor

A Parameterised constructor receives parameters at the time of creating an object and


initializes the object with the received values.

(c) Copy constructor


A constructor used to initialize the instance variables of an object by copying the initial
values of the instance variables from another object is known as Copy Constructor.

(d) Constructor overloading

The process of using a number of constructors with the same name but different types of
parameters is known as Constructor overloading.

Question 5

Why is an object not passed to a constructor by value? Explain.

Constructors are special member methods of the class. Objects are non-primitive data
types so they are passed by reference and not by value to constructors. If objects were
passed by value to a constructor then to copy the objects from actual arguments to formal
arguments, Java would again invoke the constructor. This would lead to an endless circular
loop of constructor calls.

Question 6

State the difference between constructor and method.

Constructor Method

It is a block of code that initializes a newly It is a group of statements that can be called at any point in the
created object. program using its name to perform a specific task.

It has the same name as class name. It should have a different name than class name.

It has no return type It needs a valid return type if it returns a value otherwise void

It is called implicitly at the time of object


It is called explicitly by the programmer by making a method call
creation

If a constructor is not present, a default


In case of a method, no default method is provided.
constructor is provided by Java

It is not inherited by subclasses. It may or may not be inherited depending upon its access specifier.
(Any 3 points)

Question 7

Explain two features of a constructor.

1. A constructor has the same name as that of the class.


2. A constructor has no return type, not even void.

Question 8

Differentiate between the following statements:


abc p = new abc();
abc p = new abc(5,7,9);

The first statement abc p = new abc(); is calling a non-parameterised constructor to


create and initialize an object p of class abc. The second statement abc p = new
abc(5,7,9); is calling a parameterised constructor which accepts three arguments to create
and initialize an object p of class abc.

Question 11

What are the temporary instances of a class?

If we don't assign an object created by the new operator to a variable then it is known as a
temporary instance or an anonymous object. If we want to use an object only once, then we
can use it as a temporary instance.

You might also like