Q. What is constructor.
Constructor is a special member function which has same name as class name and called whenever the object of that class is created.
Q. What are the properties of constructor
Constructor has following properties:
- Constructor has same name as class name.
- It is used for initializing variables of class.
- It is called whenever object of class is created.
- It does not have return type, not even void.
- It can have parameters.
Q. How many types of Constructors are there.
- Default Constructor --> public Message()
- No-arg Constructor --> public Message()
- Parametrized Constructor --> public Message(String str)
- Copy Constructor
Q. What are rules for writing Constructor:
- Constructor(s) of a class must has same name as the class name in which it resides.
- A constructor in Java can not be abstract, final, static and Synchronized.
- Access modifiers can be used in constructor declaration to control its access
- It can have parameters
- It should not contain return type.
- It can have throws clause: we can throw exception from constructor.
Q. What happens if you keep a return type for a constructor?
It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name
Q. How to invoke constructor.
Message m= new Message();
Q. What is the difference between constructors and other regular methods?
Constructor
|
Regular method
|
Constructors must
have the same name as the class name and cannot return a value .
public class Message {
private String msg; //constructor public Message() {}
//parameterize constructor
public Message(String str){ this.msg=str; } }
The constructors
are called only once per creation of an object.
|
Regular methods
can have any name and can be
called any
number of times.
public class Message {
//Method public String getMsg() { System.out.println("hello method"); return "TestMethod"; } }
regular methods
can be called many times.
|
Q. What happens if you do not provide a constructor?
If you do not include a constructor, the Java compiler will create a default constructor in the byte code with an empty argument. This default constructor is equivalent to the explicit constructor.
public Message() {}.
If a class includes one or more explicit constructors like
//Non-parameterize or NO-ARG constructor
public Message() {} //parameterize constructor public Message(String str){ this.msg=str; }
Then java compiler does not create the default constructor public Message() {}.
Q. How a no – argument constructor is different from default Constructor?
By default compiler creates a default constructor with no formal parameters and no throws clause.
And If you have added your own constructor (no matter whether it's without parameters or with parameters) the compiler will not add the default constructor in this case.
Q. What is Constructor Chaining ?
Constructor Chaining is calling another constructor from one constructor.
Q. How you call one constructor from another?
By using this() syntax.
Q. How to call the superclass constructor?
You can use the keyword Super to invoke the super class’s constructor.
Q. Can we declare constructor as private?
Yes we can declare constructor as private.
package com.thread; public class Message { // parameterize constructor public Message(String str) { System.out.println(str); System.out.println("String constructor"); } // parameterize constructor public Message(Integer number) { // calling another constructor this("Calling String constructor"); System.out.println("Integer constructor"); } public static void main(String[] args) { Message m2 = new Message(3); Message m3 = new Message("Test"); System.out.println(m2); System.out.println(m3); } } //Output String constructor Integer constructor Test String constructor
Q. How to call the superclass constructor?
You can use the keyword Super to invoke the super class’s constructor.
super("Your Text");
Yes we can declare constructor as private.
Q. How to write parameterize constructor?
package com.thread; public class Message { private String msg; private Integer number; // constructor public Message() { System.out.println("Empty constructor"); } // parameterize constructor public Message(String str) { System.out.println("String constructor"); this.msg = str; } // parameterize constructor public Message(Integer number) { System.out.println("Integer constructor"); this.number = number; } public static void main(String[] args) { Message m1 = new Message(); Message m2 = new Message(3); Message m3 = new Message("Test"); System.out.println(m1); System.out.println(m2); System.out.println(m3); } }
Output: Empty constructor Integer constructor String constructor
No comments:
Post a Comment