0% found this document useful (0 votes)
69 views

Constructor Questions Answer

1. A constructor in Java is a special method that initializes objects and is called when an object is created. It cannot specify a return type and its name must match the class name. 2. Constructor chaining allows constructors to call other constructors of the same class using the this() keyword or constructors of the parent class using the super() keyword. This allows multiple initialization tasks to be split across multiple constructors. 3. A default constructor is provided by the compiler if no explicit constructor is defined. It will be public and take no parameters, implicitly initializing fields to their default values. An explicit constructor prevents a default constructor from being provided.

Uploaded by

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

Constructor Questions Answer

1. A constructor in Java is a special method that initializes objects and is called when an object is created. It cannot specify a return type and its name must match the class name. 2. Constructor chaining allows constructors to call other constructors of the same class using the this() keyword or constructors of the parent class using the super() keyword. This allows multiple initialization tasks to be split across multiple constructors. 3. A default constructor is provided by the compiler if no explicit constructor is defined. It will be public and take no parameters, implicitly initializing fields to their default values. An explicit constructor prevents a default constructor from being provided.

Uploaded by

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

Constructor in Java Interview Questions

1. Define Constructor?
A constructor in Java is a special method that is used to initialize
objects/variables.
The constructor is called when an object of a class is created.
In Java, constructor role is only initializing object , and new keyword role
is crating object.

2. What Are The Rules In Defining A Constructor?


At the time of constructor declaration below points to follow:
i. Constructor name should be same as class name, and it cannot have
a return type like void.
ii. You should not declare any return type for constructor.
iii. Any no. Of constructor can be declared in a Java class but
constructor name should same as class name, and
arguments/parameters should be different.
iv. Modifiers applicable to constructor are public, private, protected
and default.
v. Final, static, abstract, synchronized not allowed.

vi. Constructor can throw exception, we can have throws clause.

3. Why we use constructor in a program?


i. To initialize data member/variables.
ii. To copy/load non-static members of a class into object----when we
create object of class.

4. Can We Define A Method With Same Name Of Class?


Yes, it is allowed to define a method with same class name. No compile
time error and no runtime error is raised, but it is not recommended as per
coding standards.

5. How Compiler And JVM Can Differentiate Constructor And Method


Definitions Of Both Have Same Class Name?
By using return type, if there is a return type it is considered as a method
else it is considered as constructor.
6. How Compiler And JVM Can Differentiate Constructor And Method
Invocations Of Both Have Same Class Name?
By using new keyword, if new keyword is used in calling then
constructor is executed else method is executed.

7. Why Return Type Is Not Allowed For Constructor?


As there is a possibility to define a method with same class name , return
type is not allowed to constructor to differentiate constructor block from
method block.

8. Why Constructor Name Is Same As Class Name?


Every class object is created using the same new keyword, so it must
have information about the class to which it must create object.
For this reason constructor name should be same as class name.

9. Can We Declare Constructor As Private?

Yes we can declare constructor as private.


All four access modifiers are allowed to constructor.
We should declare constructor as private for not to allow user to create
object from outside of our class.

10. Is Constructor Definition Is Mandatory In Class?


No, it is optional. If we do not define a constructor compiler will define
a default constructor.

11. Why Compiler Given Constructor Is Called As Default Constructor?


Because it obtain all its default properties from its class.
They are:
1. Its accessibility modifier is same as its class accessibility modifier
2. Its name is same as class name.
3. It’s does not have parameters and logic.

12. What Is Default Accessibility Modifier Of Default Constructor?


It is assigned from its class.

13. When Compiler Provides Default Constructor?


Only if there is no explicit constructor defined by developer.

14. If Class Has Explicit Constructor, Will It Has Default Constructor?


No. compiler places default constructor only if there is no explicit
constructor.

15. Can we have a Constructor in an Interface?


No, We cannot have a Constructor defined in an Interface.

16. Is it possible to call a sub class constructor from super class


constructor?

No. You cannot call a sub class constructor from a super class
constructor.

17. What is a No-arg constructor?

Constructor without arguments is called no-arg constructor. In Java


Default constructor is a no-arg constructor.
class Demo
{
public Demo()
{
//No-arg constructor
}
}

18. Can a constructor return any value?

A Constructor cannot return any explicit value but implicitly it will be


returning the instance of the class.

19. Can an abstract class in Java have a constructor?

Yes, an abstract class can have a constructor.

20. Why constructors cannot be static in Java?

When you set a method as static, it means “The Method belong to class
and not to any particular object” but a constructor is always invoked with
respect to an object, so it makes no sense for a constructor to be static.

21. Why constructors cannot be abstract in Java?

When you set a method as abstract, then “The method doesn’t or cannot
have body”. A constructor will be automatically called when object is
created. It cannot lack a body moreover an abstract constructor could
never be implemented.

22. Why constructors cannot be final in Java?

When you set a method as final, then” The method cannot be overridden
by any class”. A constructor is not inherited, so there is no need
for declaring it as final.

23. What happens when a Constructor is defined as “protected”?

If you declare constructor as protected, any other child classes or classes


within the same package can instantiate this class.

24. What are the different types of compiler?

i. Default Constructor

ii. User defined Constructor.

25. What is default constructor?

If constructor is not declared in java class then at the time of compilation


compiler will provide constructor for the class.

If programmer has declared the constructor in the class then compiler will
not provided default constructor.

The constructor provided by compiler at the time of compilation is known


as default constructor.
26. What is user defined constructor?

If programmer is declaring constructor in a java class then it is considered


to be as user defined constructor.

User defined constructor are classified into two types:

i. Without/zero parameter constructor

ii. With parameter constructor.

27. Explain with example without parameter constructor?


public class sample
{
int roll_no;
String name;
sample() //without parameter
{
roll_no = 101;
name = "Yojana";

}
public static void main(String[] args)
{
sample s = new sample();
s.display();

}
public void display()
{
System.out.println("Calling non-static
method..");
System.out.println("Roll no is:"+roll_no);
System.out.println("Name is:"+name);
}
}

28. Explain with example with parameterized constructor?


public class demo1
{
int marks;
demo1(int a) //with parameter
{
marks = a;
}
public void m1()
{
System.out.println("Marks:"+marks);
System.out.println("running m1");
}

public static void main(String[] args)


{
demo1 d1 = new demo1(100);
d1.m1();
}

29. What is constructor chaining?


Constructor chaining is the process of calling one constructor from another
constructor with respect to current object.
Constructor chaining can be done in two ways:
• Within same class: It can be done using this() keyword for constructors in
same class
• From base class: by using super() keyword to call constructor from the
base class.
Constructor chaining occurs through inheritance. A sub class constructor’s
task is to call super class’s constructor first. This ensures that creation of sub
class’s object starts with the initialization of the data members of the super
class. There could be any numbers of classes in inheritance chain. Every
constructor calls up the chain till class at the top is reached.
Why do we need constructor chaining?
This process is used when we want to perform multiple tasks in a single
constructor rather than creating a code for each task in a single constructor we
create a separate constructor for each task and make their chain which makes
the program more readable.

// Java program to illustrate Constructor Chaining

class Temp
{

Temp()
{
// calls constructor 2
this(5);
System.out.println("The Default constructor");
}

// parameterized constructor 2
Temp(int x)
{
// calls constructor 3
this(5, 15);
System.out.println(x);
}

// parameterized constructor 3
Temp(int x, int y)
{
System.out.println(x * y);
}

public static void main(String args[])


{
// invokes default constructor first
new Temp();
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy