Java / Constructor
1. What is a Constructor?
A constructor is a construct similar to java method without any return type. A constructor gets invoked when a new object is created....
2. Rules for creating a Java constructor.
Constructor name must match its class name. Constructor must not have any return type.
3. Types of Java constructor.
Two types of constructors: Default constructor (no-arg constructor). Parametrized constructor.
4. Java Default Constructor.
The constructor that takes no parameters is termed as default constructor. package com.javatutorials.urlshort; public class Fruit { Fruit() { // No parameters mentioned at open and close parenthesis....
5. Java parameterized constructor.
A constructor that takes arguments is referred as parameterized constructor.
6. Advantages of parameterized constructor.
Customize initializing the property state of the object using user defined values thus eliminates using default values.
7. What happens when the developer does not implement default constructor?
If there is no constructor in a class, compiler automatically creates a default constructor. When there are parameterized constructors implemented, compile will not create the default constructor.
8. Purpose of default constructor.
Default constructor initializes the instance variables to its default values if not initalized explicitly depending on the type. For objects, default constructor initializes it to null, int with 0....
9. Constructor Overloading in Java.
The constructor overloading is similar to method overloading in Java. Different constructors can be created for a single class.
10. Copy-Constructor.
Java supports copy constructor like C++, however Java doesn't create a default copy constructor if the developer does not provide its implementation. public class Apple { String color; String size; Apple(String color, String size) { this .color = color; this...
11. Difference between constructor and method in Java.
Java Constructor . Java Method ....
12. What are pass by reference and pass by value?
When an object is passed by value, an exact copy of the object is passed. Thus, if changes are made to that object, it doesn't affect the original object....
13. Can constructor perform other tasks instead of initialization?
Yes. In addition to initializing the state of an object, construct may initialize threads, invoke methods etc.
14. Can a constructor call/invoke a static method?
Yes.
15. Can a constructor invoke a non-static/instance method?
Yes.
16. Can abstract class have constructors?
Yes, abstract class can define constructor altough abstract class cannot be initiated.
17. Can the class with private constructor be extended?
No, in case of outer classes. The below program will generate compile time error.
18. What is the access modifier of the Default constructor?
It depends on the Class's access modifier. If the Class declared as public, then the default constructor access modifier will be public....
19. Can the inner class with private constructor be extended?
Yes. package org.javatutorials.accessModifer.protectedPackage; public class PrivateConstructorInnerClass { class Parent { private Parent() { System.out.println( "Hello from Parent" ); } } class Child extends Parent { public Child() { System.out.println( "Hello from Child" ); } } public static voi
20. Can you use this() and super() both together in a constructor?
No. Either this or super must be in first statement so we can have either one at a time and not both.
21. Can constructor be synchronized in Java?
No. constructor cannot be synchronized.
22. Is constructor inherited?
No, constructor is not inherited.
23. Can a constructor be final?
No, constructor cannot be final.
24. Can we call the constructor of a class more than once for an object in Java?
A constructor initializes a new instance. If you write none, Java adds a no-arg constructor; once you write any constructor, that default disappears.
25. Is constructor called when cloning in Java?
No. It doesn't invoke constructor.
26. Different ways to create an object in Java.
There are 5 different ways to create an object in Java. Using new keyword....
27. Difference between Class.newInstance() and Constructor.newInstance().
The Class.newInstance() method can only invoke a no-arg constructor. Quiz object = (Quiz) Class.forName("net.javapedia.quiz.model.Quiz").newInstance(); To create objects using reflection with parameterized constructor you need to use Constructor.newInstance().
28. How to Initialize class with Class.forName() that has parameterized constructor?
Use Class.getConstructor() and call Constructor.newInstance() method. For example, take the below constructor for Employee class which take 2 arguments....
29. Can you define a method with the same name as the class?
Yes, it is allowed. However not recommended due to coding standards.
30. What happens when we add a return type to the constructor?
JVM considers it as a method instead. Only the return type differentiates between constructor and the method.
31. Is Constructor definition mandatory?
Not at all However compiler will define one (default constructor) if not defined explicitly.
32. If a class has an explicit constructor, does compiler create default constructor?
No. compiler creates default constructor only if there is no explicit constructor.
33. Can we throw exceptions from a constructor?
Yes, constructor can have throws clause.
34. What are the access modifiers that cannot be applied to a constructor?
final ,static, abstract and synchronized keywords cannot be applied to a constructor.
35. What is constructor chaining?
Constructor Chaining is a technique of calling another constructor from one constructor. this () is used to call same class constructor while super () is used to call super class constructor.
36. How do I call subclass constructor from superclass constructor?
No. This is not possible.
37. What is No-arg constructor or default constructor.?
Constructor without arguments is called the no-arg constructor. Default constructor in Java is always a no-arg constructor and it is created by the compiler when no explicit constructor exists....
38. Can you declare Constructor as final in Java?
No. It results in compile time error.