Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Interview Questions on Constructors

Java Interview Questions on Constructors

  1. What is a Constructor?
    Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation.
  2. Do we have Copy Constructor in Java?
    Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.
    To copy the values of one object into another in java, you can use:
    • Constructor
    • Assigning the values of one object into another
    • clone() method of Object class
  3. What is Constructor Chaining ?
    Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor where as super() is used to call super class constructor.




    // Java program to illustrate Constructor Chaining
    // within same class Using this() keyword
    class Temp
    {
        // default constructor 1
        // default constructor will call another constructor
        // using this keyword from same class
        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();
        }
    }

    
    
  4. Can we call sub class constructor from super class constructor?
    No. There is no way in java to call sub class constructor from a super class constructor.
  5. What happens if you keep a return type for a constructor?
    Ideally, Constructor must not have a return type. By definition, if a method has a return type, it’s not a constructor.(JLS8.8 Declaration) It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name.Example:
    class GfG
    {
        int GfG()
        {
            return 0;    // Warning for the return type
        }
    }
  6. What is No-arg constructor?
    Constructor without arguments is called no-arg constructor. Default constructor in java is always a no-arg constructor.
    
    class GfG
    {
        public GfG()
        {
            //No-arg constructor
        }
    }
  7. How a no – argument constructor is different from default Constructor?
    If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

    If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

  8. What are private constructors and where are they used?
    Like any method we can provide access specifier to the constructor. If it’s made private, then it can only be accessed inside the class.
    The major scenarios where we use private constructor:
    • Internal Constructor chaining
    • Singleton class design pattern
  9. When do we need Constructor Overloading?
    Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading. Different constructors can do different work by implementing different line of codes and are called based on the type and no of parameters passed.
    According to the situation , a constructor is called with specific number of parameters among overloaded constructors.
  10. Do we have destructors in Java?
    No, Because Java is a garbage collected language you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor.

Quiz on Constructors

This article is contributed by Kiana Singh. If you like Lazyroar and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments