Q1. Can constructor throw exception?
Ans. Yes
Q2. Are Java constructors inherited? If not, why not?
Ans. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of its 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. On the other hand, constructors are by default static.
Q3. Can abstract class have constructor or not?
Ans. Abstract class can have the constructor. It is there to set the values of the abstract class's instance variables. When user (programmer) do not provide constructor, Compiler provides default constructor for abstract class just like any normal class.
Q4. Explain constructor chaining
Ans. The process of invoking the constructor from a class to super class, then its super-class and so on is known as constructor chaining.
A (String s, int s) {
// you can call only one constructor from inside of another constructor and this should be first call.
public class PolyConstructors {
Ans. The process of invoking the constructor from a class to super class, then its super-class and so on is known as constructor chaining.
If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.
NOTE: Java automatically inserts a no-argument constructor, only if there is no custom constructor in class already.
Q5. How to call one constructor from another constructor?
Ans. By using this reference
class A {
NOTE: Java automatically inserts a no-argument constructor, only if there is no custom constructor in class already.
Q5. How to call one constructor from another constructor?
Ans. By using this reference
class A {
A (String s) { }
A (int s) { }
A (String s, int s) {
// you can call only one constructor from inside of another constructor and this should be first call.
this (s);
this.i = i;
}
void abc ( String s) {
this (s) // Compiler error, as this is possible only inside constructors.
this (s) // Compiler error, as this is possible only inside constructors.
}
}
Q6. Does a class inherit the constructors of its superclass?
Ans. A class does not inherit constructors from any of its superclass, since a constructor is by default static.
Q7. Can you call an overridden method through constructor?
Ans. If you call a dynamically-bound method inside a constructor, the overridden definition for that method is used. However, the effect of this call can be rather unexpected because the overridden method will be called before the object is fully constructed. Q6. Does a class inherit the constructors of its superclass?
Ans. A class does not inherit constructors from any of its superclass, since a constructor is by default static.
Q7. Can you call an overridden method through constructor?
class Glyph {
void draw() { print("Glyph.draw()"); }
Glyph() {
print("Glyph() before draw()");
draw();
print("Glyph() after draw()");
}
}
class RoundGlyph extends Glyph {
private int radius = 1;
RoundGlyph(int r) {
radius = r;
print("RoundGlyph.RoundGlyph(), radius = " + radius);
}
void draw() {
print("RoundGlyph.draw(), radius = " + radius);
}
}
public class PolyConstructors {
public static void main(String[] args) {
new RoundGlyph(5);
}
}
/* Output:
/* Output:
Glyph() before draw()
RoundGlyph.draw(), radius = 0
Glyph() after draw()
RoundGlyph.RoundGlyph(), radius = 5
Q8. Describe conversion constructor with an example.
Ans. The Collection interface is used to pass around collections of objects where maximum generality is desired. For example, by convention all general-purpose collection implementations have a constructor that takes a Collection argument. This constructor, known as a conversion constructor, initializes the new collection to contain all of the elements in the specified collection, whatever the given collection's sub-interface or implementation type. In other words, it allows you to convert the collection's type.
Q9. Where and how can you use a private constructor?
Q8. Describe conversion constructor with an example.
Ans. The Collection interface is used to pass around collections of objects where maximum generality is desired. For example, by convention all general-purpose collection implementations have a constructor that takes a Collection argument. This constructor, known as a conversion constructor, initializes the new collection to contain all of the elements in the specified collection, whatever the given collection's sub-interface or implementation type. In other words, it allows you to convert the collection's type.
Q9. Where and how can you use a private constructor?
Ans. Private Constructor is used if you do not want other classes to instantiate the object and to prevent sub-classing. The instantiation is done by a public static method (i.e. a static factory method) within the same class.
1. Used in the singleton design pattern.
2. Used in the factory method design pattern. e.g. java.util.Collections class
3. Used in utility classes e.g. StringUtils etc.
3. Used in utility classes e.g. StringUtils etc.
No comments:
Post a Comment