Header Ads

Software testing interview questions | Constructor | Constructor vs Method | Constructor Overloading | Core Java | Tech Bharat

1. What is a constructor in Java?.

Ans. Constructor is a non static method which does not have the return type and its name should be same as the class name. If we do not declare any constructor then compiler will add a default constructor. With the help of constructor, all non static member of the class will be loaded into the object. Constructor will called automatically when an object of a class is created. Its main purpose is to initialize the objects

2. How is a constructor different from a method?

Ans.

Constructor do not have any return type as a method have.

Constructor name should be same as the class name but this is not true for method.

If we do not declare any constructor, then compiler will add a default constructor into the object. But for methods, we have to declare the method. No default method will be add into the object.

Constructor can not be inherited by  its sub-classes but a method can be inherited by its sub classes.

The only applicable modifier for constructor is public, private, default and protected. And if we use other then these access modifier then we will get compile time error but this is not the case for method. We can use any access modifier for methods

The first line of the constructor

 

3. What are the different types of constructors in Java?

Ans. Constructor can be classified into two types based on formal arguments

1. No argument constructor: A constructor does not have a formal argument is known as no argument constructor

2. Parameterized constructor: A constructor which has a formal argument is known as parameterized constructor

 

3. What is constructor overloading?

Ans.A class having more than one constructor with different formal arguments is know as constructor overloading

 

4. Can a constructor be final, static, or abstract?

Ans. No, a constructor can not be final, static or abstract. Because constructor can not be inherited, they are tied with object creation not with the class and they must be implemented and cannot be overridden

 

 

6. Can we call a constructor explicitly inside another constructor?

Ans. Yes, we can call a constructor explicitly inside another constructor using this() method

7. What happens if you don’t define a constructor in a class?

Ans. Nothing will happen. If we do not define a constructor in the class then compiler will add a default constructor into the object

Program 1 : What is a default constructor? Give one example, Write a code for that.

Ans. A constructor which do not have any formal arguments, is known as default constructor. If we do not declare any constructor, compiler will add a default constructor into the object. It is used to create and initialize the object with the default values.

 

Check out the program for default constructor Here : https://www.programiz.com/online-compiler/9zozpglXw2uc7


package practice;


public class Default_Constructor_Demo {

String name;

String platform;

Default_Constructor_Demo(){

platform="Youtube";

name="Tech Bharat";

System.out.println(name + " " + "is a "  + platform + " channel");

}


public static void main(String[] args) {

Default_Constructor_Demo obj1=new Default_Constructor_Demo();


}


}


Program 2: How do you create a parameterized constructor? Give one example. Write a code for that.

Ans. A constructor that has formal arguments is known as a parameterized constructor.

Check out the demo program: https://www.programiz.com/online-compiler/6YuYOSqKdtD2L


package practice;


public class Para_Constrcutor {

String name, Platform;

Para_Constrcutor(String name, String Platform)

{

this.name=name;

this.Platform=Platform;

System.out.println(name + " " + "is a "  + Platform + " channel");

}


public static void main(String[] args) {

Para_Constrcutor obj2= new Para_Constrcutor("Tech Bharat", "Youtube");

}


}

**********************************
Practice questions: 

Write a Java program to create five constructors inside the class and create 5 objects for these respective constructors.
  • First constructor should have no arguments
  • Second constructor should be of 1 integer
  • Third constructor should be of integer and integer
  • Fourth constructor should be of string and integer
  • Fivth constructor should be of float and string

Comment your code or GITHUB URL in the comment section

 

No comments

Powered by Blogger.