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

Constructor Interview Questions

Uploaded by

ankit.kec19
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Constructor Interview Questions

Uploaded by

ankit.kec19
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

1) Define Constructor?

Java constructor is a unique method that initializes the objects, which is called
when an instance of the class is created.
The memory for the object is allocated when we call the constructor.

Basically, a constructor is a block of code. When we create an object of the class


using the new() keyword,
at least one constructor is called, and it initializes the objects and allocates
memory to them.
If we do not specify any constructor, it will call the default constructor of the
class. However,
it is not necessary to specify an explicit constructor because the Java compiler
provides a default constructor for every Java class.
___________________________________________________________________________________
_

2) How many types of Constructors are in Java?

There are three types of constructors in Java

* Default Constructor
* Non-parameterized Constructor
* Parameterized Constructor

The syntax for the default constructor is as follows:

<class name>() {}
Example:

Employee()
{
//some code
}

The syntax for the parameterized constructor is as follows:

<class name>(arg1, arg2) {}


Example:

Employee(int i, String n)
{
id = i;
name = n;
}
________________________________________________________________________
3) Do we have a copy constructor in Java?

The following are some methods to copy the values from one object to another:

By constructor
By assigning the values of one object to another
By clone() method of Object class

Code:
[Complete the code in has a relation concept by using copy constructor ]

public class Student {


String name;
String addres;
String adharNo;
int age;
public Student() {
// TODO Auto-generated constructor stub
}
public Student(String name, String addres, String adharNo, int age) {
super();
this.name = name;
this.addres = addres;
this.adharNo = adharNo;
this.age = age;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getAddres() {


return addres;
}

public void setAddres(String addres) {


this.addres = addres;
}

public String getAdharNo() {


return adharNo;
}

public void setAdharNo(String adharNo) {


this.adharNo = adharNo;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}

@Override
public String toString() {
return "Student [name=" + name + ", addres=" + addres + ", adharNo=" +
adharNo + ", age=" + age + "]";
}

public class University {


String name;
String university_address;
Student s1;

public University() {
super();
}
University(String name,String university_name,Student s){
Student s2=new Student(s.name, s.addres,s.adharNo, s.age);
this.name=name;
this.university_address=university_name;
// s1.name=s.name;
// s1.addres=s.addres;
// s1.adharNo=s.adharNo;
//s1.age=s.age;
}

@Override
public String toString() {
return "University [name=" + name + ", university_address=" +
university_address + ", s1=" + s1 + "]";
}

public class AdmissionForm {


public static void main(String[] args) {
Student s1=new Student("abc", "hyd","12412125", 20);
University OXFORD=new University("oxford","usa",s1);
System.out.println(OXFORD);
}
}

________________________________________________________________________
4) Write a Java Program to Copy the values from one object to another Object ?

ConstructorDemo.java:

class ConstructorDemo{
int id;
String name;

ConstructorDemo(int i,String n){


id = i;
name = n;
}

ConstructorDemo(ConstructorDemo c){
id = c.id;
name =c.name;
}

void display(){
System.out.println(id+" : "+name);
System.out.println(name+" : "+name);
}

public static void main(String args[]){


ConstructorDemo c1 = new ConstructorDemo(100,"Joy");
ConstructorDemo c2 = new ConstructorDemo(c1);
c1.display();
c2.display();
}
}
______________________________________________________________________________
5) Is there any method to call a sub-class constructor from a superclass
constructor?

The subclass constructor has its own private data members, so Java does not provide
any way to access the sub-class constructor from a super class constructor.
However, we can call a superclass constructor from a sub-class constructor by using
the super keyword.
_______________________________________________________________________________
6) Can we have a constructor in the Interface?

No, we cannot have constructors in the Java interface.


_______________________________________________________________________________
7) Explain Constructor Chaining?

Constructor Chaining is a way to call one constructor from another constructor with
respect to the current object. It can be achieved in the following two ways:

From base class: We can use the super keyword to call a constructor from the base
class.

Within the same class: We can call a constructor within the same class by using
this() keyword.

Below is an example of constructor chaining:

class TestSuper
{
public TestSuper(int i)
{
System.out.println("Super Class Constructor");
}
}

class TestSub extends TestSuper


{
public TestSub()
{
this(10); //Calling same class constructor
}

public TestSub(int i)
{
super(i); //Calling super class constructor
}
}

Example 2:
---------------------
class A {
int a;
A() {
this(12);
}
A(int a) {
this(8, 9);
}

A(int a, int b) {

this.a = a;
}
}

class B extends A {
B() {
this(4);
}

B(int a) {
this(5, 6);
}

B(int a, int b) {
super();
System.out.println("hello "+super.a);
}
}

public class Sample02 {


public static void main(String[] args) {
B b = new B();
System.out.println(b.a);
}
}

___________________________________________________________________________________

8) What happens if we provide a return type to a constructor?

If we provide a return type to a constructor, it will function as a general method.


But, the compiler will display a warning message, "This method has a Constructor
name".

public class TestConstructor {


int TestConstructor()
{
return 0;
}

_________________________________________________________________________________
9) What is a private constructor?

Like methods, we can have the private constructors in Java. To make or create a
constructor as private, use the private keyword while declaring it. It can only be
accessed within that class.

The following are some usage scenarios when we need a private constructor:

Internal Constructor chaining


Singleton class design pattern
Below is an example of the private constructor:

Sample Code for singletonClass


___________________________________________
package june_25th_constructor;
/*
* can i write constructor as private
* |- yes
* it will reach a requirement name as
* singleton
*
* What is singleton class?
* |- if one and only one object is created
* within the scope of project is known as
* singleton class.
*
* My class or java class is default is a
* non singleton class!!
* multiple object can be created
*
* For making our java class as a singleton class we have to
* follow some rules and regulations.......
*
* |- stop other class to create the
* object of my class.
*
* |- only my class can create the object of my class
* if someone need the object of my class that person
* have to call the method which will return the object
* to them.
*
* |- now the users can take the object through the method
* which is present inside my class and which is returning
* the object of my class and the method is static so
* that outside class which need my object through method
* can directly call the method with the class name.
*
* |- But still we had not reached the performance of
* singleton
* |- I need a common memory area which will show the
* object is created or not and if the object is created
* return the same object and if the object is not
* created then create the new object and then return
* the new object.
*
*/

class Singleton{
private static Singleton instance=null;///1000x
private Singleton() {
System.out.println("CONSTRUCTOR CALLED THROUGH OBJECT
CREATION!!!");
}

public static Singleton getObject() {


if(instance==null) {
instance=new Singleton();
}
return instance;// 1000x
}
}
class User01{
public static void viewObject() {
Singleton obj= Singleton.getObject();
System.out.println(obj.hashCode());
}
}
class User02{
public static void viewObject() {
Singleton obj1=Singleton.getObject();
System.out.println(obj1.hashCode());
}
}
public class Program04 {
public static void main(String[] args) {
User01.viewObject();
User02.viewObject();

}
}

__________________________________________________________________________
10) Why constructors in Java cannot be static?

The constructors cannot be static in Java. When we declare a method as static, it


means the method belongs to the class and not to a specific object. But the
constructor is always invoked to the reference of objects. So, there is no sense in
making a constructor static.
__________________________________________________________________________
11) Can we make a constructor final?

No, we cannot make a constructor final. If we made a constructor final, it would


throw a compile-time error "modifier final not allowed".
__________________________________________________________________________
12) Can we make a constructor abstract?

a body, which really makes no sense. It is automatically called at the time of


object creation. So, it cannot be a block without a body.
__________________________________________________________________________
13) what will happen when a constructor is declared as protected?

Generally, when we declare a method as protected, other classes can access that
method in a different package by using inheritance only. But, when we declare a
constructor protected, it behaves slightly differently than a method. The protected
constructor can only be accessed by using a super keyword according to Java
language standards.
__________________________________________________________________________
14) Why constructor name is similar to the class name?

When we create an object of a class using a new keyword, it should have information
about that particular class. That is why the constructor's name must be similar to
the class name.
__________________________________________________________________________
15) Why return type is not allowed for the constructor?

The return type is not allowed in the constructor because if we provide a return
type in the constructor, it will act as the normal method. So, to differentiate
between constructor and method block, the return type is not allowed in
constructors.
__________________________________________________________________________
16) Can we write this() and super() in same constructor ?
___________________________________________________________________________
17) Can we override the Constructor ?
___________________________________________________________________________
18) Can we overload the Constructor ?
___________________________________________________________________________
19) How many modifiers are allowed to the constructor ?
___________________________________________________________________________
20) Can we write a return type to the constrcuctor ?
___________________________________________________________________________
21) Can we write a empty class ?
____________________________________________________________________________
22) If we write any constructor by ourself in the class then
compiler will add the constructor by itself ?

_____________________________________________________________________________
23) What is the modifiers of default constructor if program will
not write the constructor by it's own?
______________________________________________________________________________
24) Can we write super and this both in one constructor ?
______________________________________________________________________________
25) Is it possible for a class to have multiple constructors in java?
______________________________________________________________________________
26) Is it possible to inherit a constructor?
______________________________________________________________________________
27) What are possible access modifiers that can be marked for a constructor?
______________________________________________________________________________
28) When does Java compiler define the default constructor?
______________________________________________________________________________
29) Why a constructor defined by Java compiler is always called as default
constructor?
_______________________________________________________________________________
30) What will be the ouput of the following program

public class Test


{
Test(int i) {
System.out.println("Hello");
}
Test(Test test) {
System.out.println("World");
}
public static void main(String[] args)
{
Test t = new Test(new Test(20));
}
}

________________________________________________________________________________
31)
Will the below code compile successfully?
If yes, what will be the output of the following program?
public class Test
{
Test() {
this(20);
System.out.println("One");
}
Test(int i) {
this(null);
System.out.println("Two");
}
Test(Test test) {
System.out.println("Three");
}
public static void main(String[] args)
{
new Test();
}
}

__________________________________________________________________________________
32)Can we have more than one constructor with same signature in a class?
__________________________________________________________________________________
33)Can we create an object of class within the same class if a constructor is
marked with private?
_________________________________________________________________________________
34) Can class be extended when a constructor is declared private?
__________________________________________________________________________________
35) What is the difference between constructor and method?

You might also like