More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Derived object carries the body of its class as well as the body of the parent class. Its body ( member elements ) is initialized using its own class constructor whereas the body ( member elements ) carried from the parent class are initialized using super class constructor. So In order to initialize the elements of the parent class before its own elements are even initialized, super is called.
Help us improve. Please let us know the company, where you were asked this question :
Ans. New operator in Java creates objects. Constructor is the later step in object creation. Constructor's job is to initialize the members after the object has reserved memory for itself.
Help us improve. Please let us know the company, where you were asked this question :
Ans. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to override the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Though It's often confused with each other, Object Creation ( Instantiation ) and Initialization ( Construction ) are different things in Java. Construction follows object creation.
Object Creation is the process to create the object in memory and returning its handler. Java provides New keyword for object creation.
Initialization is the process of setting the initial / default values to the members. Constructor is used for this purpose. If we don't provide any constructor, Java provides one default implementation to set the default values according to the member data types.
Help us improve. Please let us know the company, where you were asked this question :
Ans. 1.this is a reference to the current object in which this keyword is used whereas super is a reference used to access members specific to the parent Class.
2.this is primarily used for accessing member variables if local variables have same name, for constructor chaining and for passing itself to some method whereas super is primarily used to initialize base class members within derived class constructor.
Help us improve. Please let us know the company, where you were asked this question :
Ans. A child object constructor always first needs to construct its parent. In Java it is done via an implicit call to the no-args constructor as the first statement
Help us improve. Please let us know the company, where you were asked this question :
Ans. At the beginning of an object's life, the Java virtual machine (JVM) allocates memory on the heap to accommodate the object's instance variables. When that memory is first allocated, however, the data it contains is unpredictable. If the memory were used as is, the behavior of the object would also be unpredictable. To guard against such a scenario, Java makes certain that memory is initialized, at least to predictable default values before it is used by any code.
Help us improve. Please let us know the company, where you were asked this question :
Constructor provided by Java if no constructor is declared
Constructor with empty body
All of the above
Default Constructor is provided by Java ...
To Reserve Memory
To provide at least one instance method
To Make it look good
To initialize the object state
Q13. Which of the following is false about Constructors ?
a. Constructor can be overloaded b. A no argument constructor is provided by the compiler if we declare only constructors with arguments. c. Constructors shouldn't have any return types , not even void. d. If super is not explicitly called, still super() is intrinsically added by the compiler.
Ans. Compile time error as it won't find the constructor matching BuggyBread2().
Compiler won't provide default no argument constructor as programmer has already defined one constructor.
Compiler will treat user defined BuggyBread2() as a method, as return type ( void ) has been specified for that.
Help us improve. Please let us know the company, where you were asked this question :
How can we create objects if we make the constructor private ?
We can't create objects if constructor is private
We can only create objects if we follow singleton pattern
We can only create one object
We can create new object through static method or static block
What will be the output of following code ?
public class BuggyBread {
private int x;
private Integer y;
private BuggyBread(int x,int y){};
public static void main(String[] args){
BuggyBread buggybread = new BuggyBread(1,2);
System.out.println(buggybread.x);
}
}
compilation error due to private constructor
compilation error due to uninitialized elements
0 null
0 0
Q23. Difference between Class#getInstance() and new operator ?
Ans. Class.getInstance doesn't call the constructor whereas if we create an object using new operator , we need to have a matching constructor or copiler should provide a default constructor.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No. Every Class only needs to have one constructor - With parameters or without parameters. Compiler provides a default non parameterized constructor if no constructors is defined.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java expects the superclass ( Object Class ) constructor to be called while creation of any object. So super constructor is called in case there are no instance variables to initialize.
Help us improve. Please let us know the company, where you were asked this question :
Q29. Is it advisable to set the member variables through constructors instead of setting them through setters ?
Ans. Yes , If the values to be set are known at the time of initialization and doesn't involve polymorphic behavior.If it's using Dependency Injection , then Constructor injection must be available. If it suffice the above conditions, then definitely its advisable to have them set through constructor as they eagerly load the values into the memory and save it fro multiple values assignment ( one through default constructor and then through assignment )
Help us improve. Please let us know the company, where you were asked this question :
Q30. What will happen if we have our own constructor but only initialize some of the member elements ?
Ans. Its always advisable to initialize all the member elements in case we don't use the compiler's default constructor as leftover member elements will have garbage values and may reflect and unstable state for the object.
Help us improve. Please let us know the company, where you were asked this question :